【问题标题】:AWS S3 PHP can I upload a file directly to a folder in a bucketAWS S3 PHP我可以将文件直接上传到存储桶中的文件夹吗
【发布时间】:2018-11-28 13:27:09
【问题描述】:

使用 php sdk 将文件直接上传到 AWS S3 中的文件夹的语法是什么?

我成功上传到存储桶,使用:

$response = $s3->create_object($bucket, $file_name, array('fileUpload' => $file_path))

发送!

【问题讨论】:

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


    【解决方案1】:

    试试这个方法

    $s3->create_object($bucket, 'folder/filename', $options)
    

    【讨论】:

    • 是的,请记住 S3 中没有文件夹。您可以使用类似文件夹的名称(例如 folder/filename)来呈现文件层次结构,但这些都是普通数据存储桶中的简单文件。因此,您需要在文件名前显式添加您想要使用的任何类似文件夹的标识符。
    【解决方案2】:

    只需做 1 件事,添加 'path/' 例如:如果您在存储桶中有一个文件夹 'videos',那么您必须在 'key' =>'vid​​eos/video-name-here' 中添加这样的路径。

    try {
        // Upload data.
    $result = $s3Client->putObject([
        'Bucket' => bucket-name,
        'Key'    => 'videos/video-name', //add path here
        'Body'   => fopen($video, 'r'),
        'ACL'    => 'public-read'
    ]);
    print_r($result['ObjectURL']);
    
    } catch (S3Exception $e) {
        echo $e->getMessage() . PHP_EOL;
    }
    

    【讨论】:

      【解决方案3】:

      这是您可以尝试的另一种方法

          $this->Aws->bucket = 'bucket'
          $old_file = $dir. '/'.old_file_name;       
          $new_file = $dir. '/'.new_file_name;
          $file_permission = 'private';
          $this->Aws->upload($old_file , $new_file , $file_permission);
      

      【讨论】:

        猜你喜欢
        • 2020-10-10
        • 2018-04-25
        • 1970-01-01
        • 2019-10-23
        • 2018-08-13
        • 2020-03-17
        • 2021-02-07
        • 2018-09-19
        • 1970-01-01
        相关资源
        最近更新 更多