`
阅读更多
Back in the ‘burgh at Carnegie Mellon’s radio station WRCT, a really awesome poster with a baby crawling to its death in a bucket reminded you of how dangerous buckets can really be. Especially when DJing techno.  I can’t work with Amazon S3 without wishing all my baby resources and folders and files would crawl in to an S3 death bucket and vanish. OMG AMAZON!

So anyhoo, to create folders instead of just files (or what appears to be folders in the awesome S3 firefox organizer), do a PUT with:

•header content_type to binary/octet-stream
•append _$folder$ to the end of the path name (as the extension)
•set the data to blank
This bitty rails code demonstrates this using the S3 plugin:


  S3_FOLDER_EXT = '_$folder$'
  def exists_in_S3?(file_name, bucket_name, options ={})
    connect_to_S3

    file_search_name = file_name.dup
    file_search_name << S3_FOLDER_EXT if options[:folder]

    AWS::S3::Bucket.objects(bucket_name, :prefix => file_search_name).any?

  end

  def create_S3_folder(file_name, bucket_name, options={})
    connect_to_S3
    folder = AWS::S3::S3Object.store(file_name + S3_FOLDER_EXT, '', bucket_name,
      {:access => :public_read,
       :content_type => "binary/octet-stream",
       :cache_control => 'max-age=3600,post-check=900,pre-check=3600' }.update(options))
  end

  def connect_to_S3
    unless AWS::S3::Base.connected?
      AWS::S3::Base.establish_connection!(:access_key_id => 'blah', :secret_access_key => 'giraffe!')
    end
  endSo your request path would look like:
/baby_bucket?prefix=folder1/folder2_$folder$
And the output:

<ListBucketResult xmlns=”http://s3.amazonaws.com/doc/2006-03-01/”>
<Name>baby_bucket</Name>
<Prefix>folder1/folder2_$folder$</Prefix>
<Marker></Marker>
<MaxKeys>1000</MaxKeys>
<IsTruncated>false</IsTruncated>
</ListBucketResult>


At this point, you should ask yourself why you want to create folders in the first place. Because I want to stick my head in a bucket right now.


=========================================================
how can i create a folder under a bucket using boto library for amazon s3, i followed the manual, and create keys with contents with permission, metadata etc, but no where in the boto's documentation say how to create folders under bucket, or create folder under folders in bucket.
amazon-s3 boto
link|flag edited Dec 21 '09 at 12:11
skaffman
62.7k642100 asked Dec 21 '09 at 12:04
vito huang
1348

86% accept rate

  it seems amazon s3 doesn't have concept of folder, some suggest creating key with name like "folder/test.txt' to get around it. i tried using firefox s3 plugin to create folder, and list all keys in boto, it outputs the folder i just created as "<Key: vitoshares,everyone_$folder$>], so how can i view/add/modify content to/from this folder? – vito huang Dec 21 '09 at 12:29

2 Answersactive
newest
votes 
up vote
3

down vote
accepted  Yes, thers no folder concept in S3, you can create file names like "abc/xys/uvw/123.jpg" which many S3 access tools like S3Fox show like a directory structure. But actually the its a single file in a bucket.

Amey Kanade
link|flag answered Jan 26 at 18:03
user240469
514

  thanks for the answer, so i guess if i want to see the contents of a particular folder, i will need to traverse lots other unnecessary files? – vito huang Jan 27 at 23:50
The S3 API lets you query a bucket for keys with a given prefix. So you should be able to construct a request that only returns objects in your logical folder. See: docs.amazonwebservices.com/AmazonS3/2006-03-01/dev/… – James Cooper Jul 22 at 19:58



up vote
1

down vote  Hey,

Amazon S3 indeed doesn't have a concept of the folders. As you correctly mentioned you need to create keys with slash symbol to mimic that. You can use CloudBerry Explorer freeware to create folders separated by '/' rather than with $folder$ suffix.


==================================================
Create a directory on S3 - how?
Posted by: Luis Batista - Dom Digital
Posted on: Mar 9, 2010 9:23 AM     Reply

 

This question is not answered. Helpful answers available: 2. Correct answers available: 1. 

Hello,

A newbie question, how create a directory (folder) on a bucket ?

I use this code:
--------------------------------
foreach (FileInfo FI in FileList)
{
  AmazonS3 clientS3 = Amazon.AWSClientFactory.CreateAmazonS3Client(accessKeyId, secretAccessKey);
    try
    {
      PutObjectRequest request = new PutObjectRequest();
      request.WithFilePath(FI.DirectoryName + "
" + FI.Name)
      .WithBucketName(BucketName)
      .WithKey(MyDirectory + "/" + FI.Name)
      .WithCannedACL(S3CannedACL.PublicRead);
      S3Response response = clientS3.PutObject(request);
      response.Dispose();
   }
   catch (AmazonS3Exception ex)
   {
      if (ex.ErrorCode \!= null && (ex.ErrorCode.Equals("InvalidAccessKeyId") || ex.ErrorCode.Equals("InvalidSecurity")))
      {
         Console.WriteLine("Please check the provided AWS Credentials.");
         Console.WriteLine("If you haven't signed up for Amazon S3, please visit http://aws.amazon.com/s3");
      }
      else
      {
         Console.WriteLine("An error occurred with the message '{0}' when writing an object", ex.Message);
      }
   }       
}   

---------------------------
In "MyDirectory" variable I put the name of folder I want on bucket.
I use the firefox pluglin "S3 Organizer" to manage files on Amazon S3, if I delete the file, the directory is removed, is like the name of file include the directory name!

Any help with this situation?

Regards,
DD




  Replies: 3 | Pages: 1 - Last Post: Mar 12, 2010 7:48 AM by: Colin Rhodes 


Replies 

Re: Create a directory on S3 - how?
Posted by:  Colin Rhodes
Posted on: Mar 9, 2010 11:12 AM
in response to: Luis Batista - Dom Digital     Reply

 

There is no such thing as directories on S3. Many S3 clients simulate folders by detecting '/' characters in the keys of the objects on S3.

Some clients will allow you to create empty objects with keys that end in a '/'. Some clients use these objects as a folder placeholder. 



Re: Create a directory on S3 - how?
Posted by: Luis Batista - Dom Digital
Posted on: Mar 11, 2010 4:56 AM
in response to: Colin Rhodes     Reply

 

Hello,

You can put a example code how create a empty object with "/" and then put objects in this "placeholder" ?


Regards,
DD


Message was edited by: Luis Batista - Dom Digital 



Re: Create a directory on S3 - how?
Posted by:  Colin Rhodes
Posted on: Mar 12, 2010 7:48 AM
in response to: Luis Batista - Dom Digital     Reply

 

When I use S3, I don't create placeholder "folder" objects because I have no use for them. My main data bucket on S3 has the data broken down into sets of files associated with an ID, where the ID is the "folder".

When I want to write a data object, I simply write to "{id}/{filename}". I have no need to create a placeholder "folder" object.

The important thing to understand is that the placeholder "folder" doesn't do anything except when visualizing the structure. You don't add objects to a folder -- you just create them with the desired prefix.

That said, if you really want to create a placeholder, you could write an empty string object to the key 'myfolder/'. If an empty string doesn't work, add a single character. 



==========================================================
Using .NET AWS SDK to create folder in S3 bucket
Posted by: gopinatht
Posted on: Aug 30, 2010 12:00 PM     Reply

 

Hi All,

I apologize if this question has been asked before but I did a search and I could not find a good response.

I am trying to create a folder in a bucket in S3 using rhe AWSSDK .NET API. Here is my code:

PutObjectRequest putReq = new PutObjectRequest();
putReq.BucketName = "MyUniqueBucket";
putReq.WithKey("FromApi/");
putReq.WithContentType("binary/octet-stream");
s3Client.PutObject(putReq);

When I do this, I get the following exception:

An unhandled exception of type 'System.ArgumentException' occurred in AWSSDK.dll

Additional information: Please specify either a Filename, provide a FileStream or provide a ContentBody to PUT an object into S3.

What am I doing wrong? How can I create the folder?

Thanks in advance.

Best Regards
Gopinath



  Replies: 1 | Pages: 1 - Last Post: Aug 31, 2010 12:05 AM by: Norm@AWS 


Replies 

Re: Using .NET AWS SDK to create folder in S3 bucket
Posted by:  Norm@AWS
Posted on: Aug 31, 2010 12:05 AM
in response to: gopinatht     Reply

 

S3 doesn't support folders in a bucket.  Many tools simulate folders by adding a 0 byte object when the folder is first being "created" and then removing the 0 byte file once the first object is added to the simulated folder. 

In your example you create the folder by putting a 0 byte object with an object key "FromAPI_$folder$".  Then later objects would be added with the object key set to something like "FromAPI/FirstObject". 

Norm




分享到:
评论

相关推荐

    netmfawss3:这是一个用于处理 Amazon S3 Rest API 的 .NET Micro Framework Library

    这是用于 Amazon S3 Rest API 的 .NET Micro Framework 库。 您可以使用此库执行以下操作: 创建存储桶(容器、文件夹) 删除存储桶 创建对象(文件) 删除对象 由于 AWS 服务的高度安全性,库内有许多 SHA256 ...

    rdiot-p022:Pi4J + Amazon S3 REST API + S3 Lambda触发器+ DynamoDB [P022]

    在本项目"rdiot-p022"中,我们探讨了如何将Raspberry Pi(以Pi4J库的形式)与Amazon Web Services (AWS) 的关键组件集成,包括S3 REST API、S3 Lambda触发器以及DynamoDB。这是一个基于JavaScript的解决方案,用于...

    亚马逊s3-api文档.pdf

    亚马逊S3 API文档是Amazon Simple Storage Service (S3)的官方参考手册,它详细描述了如何使用S3的REST API进行数据的存储和检索。文档中包含了关于如何操作S3的基本概念、使用方法、身份验证以及数据类型等重要信息...

    亚马逊S3接口文档

    亚马逊S3 REST API提供了访问存储桶(Bucket)和对象(Object)的标准方法。它是无状态的,并且可以同时支持多个不同的应用程序。开发者可以通过REST API与S3服务进行交互。文档中提到的API版本是2006-03-01,这是S3...

    s3-api.pdf

    总结来说,Amazon S3 API中文文档是一份详尽的参考资料,不仅提供了如何通过REST API与S3服务进行交互的技术细节,还特别强调了安全性措施,如请求签名的必要性和实现方法。此外,文档还提供了一系列实例,帮助...

    Amazon S3 Api Manual

    ## Amazon S3 REST API 介绍 Amazon Simple Storage Service (S3) 是一种对象存储服务,旨在为互联网规模的应用程序提供高可用性、高性能以及低成本的数据存储解决方案。本手册主要介绍了如何通过 REST API 来与 ...

    TccInspetorAPI:Java中的REST API可在Amazon S3上上传文件

    TccInspetorAPI 这是一个Java API,提供了一种将对象上传到Amazon S3存储桶的方法。 它对应于以下体系结构的TccInspetorAPI组件: 它是在Spring Boot,Jersey和Maven开发的。 它部署在Heroku中。设置部署环境Heroku...

    yoke.engine.jade-1.0.18.zip

    "s3-spraysupport" 意味着这个项目包含对S3服务的 Spray 客户端支持,使得开发者能够通过Spray轻松地与S3 REST API进行交互,进行上传、下载、管理对象等操作。 【标签】"开源项目" 指出这个资源是开放源代码的,...

    Python库 | pyss3-0.5.3-py3-none-any.whl

    它使用了AWS的S3 REST API,确保与AWS服务的兼容性。 在后端开发中,`pyss3`可以作为存储解决方案,用于备份、分发或存储应用程序的数据。开发者可以结合其他Python库,如`boto3`(官方的AWS SDK for Python),...

    诺克斯:S3 Lib

    以下示例演示了knox和S3 REST API的某些功能。 首先,创建一个S3客户端: var client = knox . createClient ( { key : '&lt;api&gt;' , secret : '&lt;secret&gt;' , bucket : 'learnboost' } ) ; 下面记录了更多选项,...

    noxmox:Amazon S3客户端和Node.js的实体模型(未维护)

    该库提供了两个实现Amazon S3 REST API函数的node.js模块: nox一个简单的Amazon S3客户端,支持现有存储桶的put() , get()和del()方法。 mox一个简单的S3模型,支持与nox相同的方法,可在本地驱动器上模拟Amazon ...

    CakeS3:用于Amazon S3文件存储的CakePHP 2.0插件

    此类是适用于PHP 5.2.x(使用CURL)的独立Amazon S3 REST实现,支持大型文件上传,并且不需要PEAR。 这意味着它可以放入各种托管平台中并在其上运行。安装执行以下操作之一在CakePHP 2.0应用程序中使用此插件 git ...

    AWS S3对象存储手册

    S3的应用程序编程接口(API)提供了两种主要的接口:REST接口和SOAP接口。这两种接口为开发者提供了灵活的方式来与S3服务进行交互。S3是按使用付费的服务,用户根据存储空间使用量、请求次数、数据传出量等进行付费...

    AWS S3 对象云存储。SDK msvc_x64下使用,vs2019编译 debug库。

    提供了统一的接口 REST/SOAP 来统一访问任何数据 对 S3 来说,存在里面的数据就是对象名(键),和数据(值) 不限量,单个文件最高可达 5TB 高速。每个 bucket 下每秒可达 3500 PUT/COPY/POST/DELETE 或 5500 GET/...

    Amazon S3 api

    REST API是S3服务的编程接口,允许开发者使用HTTP的GET、PUT、DELETE等方法与S3服务交互。 **2. 请求和响应头** 文件中提到了常见的请求头和响应头,这些是在进行HTTP请求时必须包含或可选择包含的字段。比如,常见...

    s2s3h4项目

    2. RESTful API设计:构建服务间的通信接口,遵循REST原则,确保服务间的交互简洁高效。 3. 消息队列:如RabbitMQ或Kafka,用于解耦服务间的通信,实现异步处理,提高系统的可扩展性。 4. 服务注册与发现:如Consul...

    amazon-s3-php-class:适用于PHP 5CURL的独立Amazon S3(REST)客户端

    $ s3 = new S3 ( $ awsAccessKey , $ awsSecretKey ); 静态地(例如,S3 :: getObject(...)): S3 :: setAuth ( $ awsAccessKey , $ awsSecretKey ); 对象操作 上载物件 放置文件中的对象: S3 :: putObject ...

    rest_ful api.rar

    这里的"rest_ful api.rar"压缩包显然包含了使用Java编程语言实现的AWS SDK v2版本针对S3(Simple Storage Service)的操作示例。AWS S3是Amazon Web Services提供的一个高度可扩展、低成本的对象存储服务,可以存储...

Global site tag (gtag.js) - Google Analytics