为了账号安全,请及时绑定邮箱和手机立即绑定

如何使用 PHP AWS S3 API 创建动态键名

如何使用 PHP AWS S3 API 创建动态键名

PHP
慕运维8079593 2022-09-12 08:31:59
一直在测试最新的Php AWS S3 API,我不确定如何在用户上传新书时动态命名密钥名称,原因是我需要能够检索XYZ客户上传的书。提前致谢! <?php  require 'aws/aws-autoloader.php';  use Aws\S3\S3Client;  use Aws\S3\Exception\S3Exception;  //AWS S3 SHITE BELOW   $bucket = 'acmebooks';  //CREATE DYNAMIC KEYNAME??  $keyname = 'RANDOM KEYNAME';  $secret = 'FOOBAR1345';  $credentials = new Aws\Credentials\Credentials($keyname, $secret);  $s3 = new Aws\S3\S3Client([  'version'     => 'latest',  'region'      => 'us-east-2',  'credentials' => $credentials  ]);  try {  // Upload data.   $result = $s3->putObject([    'Bucket' => $bucket,    'Key'    => $keyname,    //Body'   => 'Hello, world!',    'Body'   => 'https://booksrun.com/image-loader/350/https:__images-na.ssl-images-amazon.com_images_I_41sYJq3nAWL.jpg',    'ACL'    => 'public-read']);// Print the URL to the object.echo $result['ObjectURL'] . PHP_EOL;print_r($result['Body']);} catch (S3Exception $e) {    echo $e->getMessage() . PHP_EOL;}?> 
查看完整描述

1 回答

?
婷婷同学_

TA贡献1844条经验 获得超8个赞

忽略和混淆$keyname这是我唯一的AWS S3“密钥”,用于放入AWS S3存储桶的“密钥名称”。


修复以下内容


<?php

 require 'aws/aws-autoloader.php';


 use Aws\S3\S3Client;

 use Aws\S3\Exception\S3Exception;


 //TEST FOR AWS S3

 $key_input = 'jaybookcover1';


 //AWS S3 SHITE BELOW 

 $bucket = 'acmebooks';

 //UNIQUE AWS S3 KEYNAME??

 $keyname = ' my unique AWS S3 key';

 $secret = 'FOOBAR1345';


 $credentials = new Aws\Credentials\Credentials($keyname, $secret);


 $s3 = new Aws\S3\S3Client([

 'version'     => 'latest',

 'region'      => 'us-east-2',

 'credentials' => $credentials

 ]);


 try {

 // Upload data.

 $result = $s3->putObject([

'Bucket' => $bucket,

'Key'    => $key_input,

//Body'   => 'Hello, world!',

'Body'   => 'https://booksrun.com/image-loader/350/https:__images-na.ssl-images-amazon.com_images_I_41sYJq3nAWL.jpg',

'ACL'    => 'public-read'

]);


// Print the URL to the object.

echo $result['ObjectURL'] . PHP_EOL;

print_r($result['Body']);

} catch (S3Exception $e) {


echo $e->getMessage() . PHP_EOL;


}

?>      


查看完整回答
反对 回复 2022-09-12
  • 1 回答
  • 0 关注
  • 108 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信