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

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


}

?> 


慕运维8079593
浏览 109回答 1
1回答

婷婷同学_

忽略和混淆$keyname这是我唯一的AWS S3“密钥”,用于放入AWS S3存储桶的“密钥名称”。修复以下内容<?php&nbsp;require 'aws/aws-autoloader.php';&nbsp;use Aws\S3\S3Client;&nbsp;use Aws\S3\Exception\S3Exception;&nbsp;//TEST FOR AWS S3&nbsp;$key_input = 'jaybookcover1';&nbsp;//AWS S3 SHITE BELOW&nbsp;&nbsp;$bucket = 'acmebooks';&nbsp;//UNIQUE AWS S3 KEYNAME??&nbsp;$keyname = ' my unique AWS S3 key';&nbsp;$secret = 'FOOBAR1345';&nbsp;$credentials = new Aws\Credentials\Credentials($keyname, $secret);&nbsp;$s3 = new Aws\S3\S3Client([&nbsp;'version'&nbsp; &nbsp; &nbsp;=> 'latest',&nbsp;'region'&nbsp; &nbsp; &nbsp; => 'us-east-2',&nbsp;'credentials' => $credentials&nbsp;]);&nbsp;try {&nbsp;// Upload data.&nbsp;$result = $s3->putObject(['Bucket' => $bucket,'Key'&nbsp; &nbsp; => $key_input,//Body'&nbsp; &nbsp;=> 'Hello, world!','Body'&nbsp; &nbsp;=> 'https://booksrun.com/image-loader/350/https:__images-na.ssl-images-amazon.com_images_I_41sYJq3nAWL.jpg','ACL'&nbsp; &nbsp; => '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;}?>&nbsp; &nbsp; &nbsp;&nbsp;
打开App,查看更多内容
随时随地看视频慕课网APP