如何通过 PHP SDK 在 Amazon S3 存储桶下创建文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12291706/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:00  来源:igfitidea点击:

How to create folder under Amazon S3 bucket through PHP SDK?

phpamazon-s3amazon

提问by 100calorie

I am developing an iPhone app to allow user upload photo and share. I want to use S3 to store uploaded images as well as processed images (foe example, thumbnail, reduced size image). I've installed AWS PHP SDK on my EC2 instance, my questions are: 1) Should photos uploaded from iPhone app go to a EC2 directory first, then copied over to S3 bucket, or it should directly uploaded to S3? 2) How can I create different folders under the S3 bucket through PHP SDK and how to read the files from the folder?

我正在开发一个 iPhone 应用程序来允许用户上传照片和分享。我想使用 S3 来存储上传的图像以及处理过的图像(例如,缩略图、缩小尺寸的图像)。我已经在我的 EC2 实例上安装了 AWS PHP SDK,我的问题是:1) 从 iPhone 应用程序上传的照片应该先转到 EC2 目录,然后复制到 S3 存储桶,还是应该直接上传到 S3?2) 如何通过 PHP SDK 在 S3 存储桶下创建不同的文件夹以及如何从文件夹中读取文件?

Thanks in advance!

提前致谢!

采纳答案by alfredaday

  1. The answer can be found here: Direct upload to s3 without the use of a production server

  2. I've never used the PHP SDK, but I was browsing through the AWS SDK for PHP 1.5.14 documentationand came across the following APIs that you will need to utilize:

    a) create_object: You'll use this to put a object into a bucket. You'll specify the filename. You asked how you can create different folders: you will include the full path into the filename. For instance instead of naming the file "photo1.jpg", you would name it "user1/vacation/photo1.jpg".

    b) get_object_list: This API will return to you a list of objects given some criteria. If you want to read all the objects from a particular folder, specify the prefix as the path to the folder. For instance, if I want to find all files in the folder "user1/vacation/", I would specify my prefix to be "user1/vacation/".

  1. 答案可以在这里找到: Direct Upload to s3 without using a production server

  2. 我从未使用过 PHP 开发工具包,但我浏览了适用于 PHPAWS 开发工具包 1.5.14 文档并遇到了您需要使用的以下 API:

    a) create_object:您将使用它来将对象放入存储桶中。您将指定文件名。您询问如何创建不同的文件夹:您将在文件名中包含完整路径。例如,不是将文件命名为“photo1.jpg”,而是将其命名为“user1/vacation/photo1.jpg”。

    b) get_object_list:此 API 将根据某些条件返回给您一个对象列表。如果要读取特定文件夹中的所有对象,请将前缀指定为文件夹的路径。例如,如果我想在文件夹“user1/vacation/”中查找所有文件,我会将我的前缀指定为“user1/vacation/”。

回答by Manoj Prajapat

Yes, it is possible to create new folder using s3sdk.

是的,可以使用s3sdk创建新文件夹。

try bellow code

试试下面的代码

<?php

 /* abc is the folder name */

 $s3->putObject(array( 
                   'Bucket' => $bucket,
                   'Key'    => "abc/",
                   'Body'   => "",
                   'ACL'    => 'public-read'
                  ));