【问题标题】:How Do I Create a Dynamic Keyname Using Php AWS S3 API如何使用 PHP AWS S3 API 创建动态键名
【发布时间】:2020-05-23 05:42:46
【问题描述】:

一直在测试最新的 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;

}
?>      

【问题讨论】:

    标签: php amazon-web-services api amazon-s3


    【解决方案1】:

    忽略并混淆了 $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;
    
    }
    ?>      
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2018-12-08
      • 2023-03-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2022-01-03
      • 1970-01-01
      相关资源
      最近更新 更多