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:

public void CreateFolder(stringbucket, stringfolder)
{
    var key =string.Format(@"{0}/", folder);
    var request =new PutObjectRequest().WithBucketName(bucket).WithKey(key);
    request.InputStream =new MemoryStream();
    _client.PutObject(request);
}

   

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 theS3 objects returned in the response, some­thing along the line of:

// get the objects at the TOP LEVEL, i.e. not inside any folders
var objects = response.S3Objects.Where(o => !o.Key.Contains(@"/"));
 
// get the folders at the TOP LEVEL only
var folders = response.S3Objects.Except(objects)
                      .Where(o => o.Key.Last() =='/' &&
                                  o.Key.IndexOf(@"/") == o.Key.LastIndexOf(@"/"));


原文地址:https://www.cnblogs.com/puncha/p/3876932.html