AWS之S3实践

从S3的API上来看,S3的资源分为Bucket和Object 2种类型。Bucket是S3根对象(如果S3是根目录的话,那么Bucket就是根目录下的文件夹),所有位于Bucket下的资源都是Object,但是其实Object可以分成目录和文件2种(虽然从API上是区分不出的,他们都是Object),但是可以通过他们的名字来区分,目录一定是以/结尾,而文件末尾肯定没有/。这个也是S3的API用以区分folder和file的关键。

这篇文件可以帮助理解:http://theburningmonk.com/2011/01/working-with-s3-folders-using-the-net-aws-sdk/


If you’ve been using S3 client in the AWS SDK for .Net you might have noticed that there are no meth­ods that let you inter­act with the fold­ers in a bucket. As it turns out,S3 does not sup­port fold­ers in the con­ven­tional sense*, every­thing is still a key value pair, but tools such asCloud Berry or indeed the Ama­zon web con­sole sim­ply uses ‘/’ char­ac­ters in the key to indi­cate a folder structure.

This might seem odd at first but when you think about it, there are no folder struc­ture on your hard drive either, it’s a log­i­cal struc­ture theOS pro­vides for you to make it eas­ier for us mere mor­tals to work with.

Back to the topic at hand, what this means is that:

  • if you add an object with key myfolder/ to S3, it’ll be seen as a folder
  • if you add an object with key myfolder/myfile.txt to S3, it’ll be seen as a file myfile.txt inside a myfolder folder, if the folder object doesn’t exist already it’ll be added automatically
  • when you make a Lis­tO­b­jects call both myfolder/ and myfolder/myfile.txt will be included in the result
Cre­at­ing folders

To cre­ate a folder, you just need to add an object which ends with ‘/’, like this:

[csharp]  view plain copy print ?
  1. public void CreateFolder(stringbucket, stringfolder)  
  2. {  
  3.     var key =string.Format(@"{0}/", folder);  
  4.     var request =new PutObjectRequest().WithBucketName(bucket).WithKey(key);  
  5.     request.InputStream =new MemoryStream();  
  6.     _client.PutObject(request);  
  7. }  

  

Here is a thread on the Ama­zon forum which cov­ers this technique.

List­ing con­tents of a folder

With the Lis­tO­b­jects method on the S3 client you can pro­vide a pre­fix require­ment, and to get the list of objects in a par­tic­u­lar folder sim­ply add the path of the folder (e.g. topfolder/middlefolder/) in the request:

var request = new ListObjectsRequest().WithBucketName(bucket).WithPrefix(folder);

If you are only inter­ested in the objects (includ­ing fold­ers) that are in the top level of your folder/bucket then you’d need to do some fil­ter­ing on theS3objects returned in the response, some­thing along the line of:

[csharp]  view plain copy print ?
  1. // get the objects at the TOP LEVEL, i.e. not inside any folders  
  2. var objects = response.S3Objects.Where(o => !o.Key.Contains(@"/"));  
  3.    
  4. // get the folders at the TOP LEVEL only  
  5. var folders = response.S3Objects.Except(objects)  
  6.                       .Where(o => o.Key.Last() =='/' &&  
  7.                                   o.Key.IndexOf(@"/") == o.Key.LastIndexOf(@"/"));  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值