Amazon S3 Creating Folder through .NET SDK vs through Management Console

泪湿孤枕 提交于 2020-03-13 06:12:07

问题


I'm trying to determine if a folder exists on my Amazon S3 Bucket and if it doesn't I want to create it.

At the moment I can create the folder using the .NET SDK as follows:

        public void CreateFolder(string bucketName, string folderName)
    {
        var folderKey = folderName + "/"; //end the folder name with "/"

        var request = new PutObjectRequest();

        request.WithBucketName(bucketName);

        request.StorageClass = S3StorageClass.Standard;
        request.ServerSideEncryptionMethod = ServerSideEncryptionMethod.None;

        //request.CannedACL = S3CannedACL.BucketOwnerFullControl;

        request.WithKey(folderKey);

        request.WithContentBody(string.Empty);

        S3Response response = m_S3Client.PutObject(request);

    }

Now when I try to see if the folder exists using this code:

        public bool DoesFolderExist(string key, string bucketName)
    {
        try
        {
            S3Response response = m_S3Client.GetObjectMetadata(new GetObjectMetadataRequest()
               .WithBucketName(bucketName)
               .WithKey(key));

            return true;
        }
        catch (Amazon.S3.AmazonS3Exception ex)
        {
            if (ex.StatusCode == System.Net.HttpStatusCode.NotFound)
                return false;

            //status wasn't not found, so throw the exception
            throw;
        }
    }

It cannot find the folder. The strange thing is if I create the folder using the AWS Management Console, the 'DoesFolderExist' method can see it.

I'm not sure if it's an ACL/IAM thing but am not sure how to resolve this.


回答1:


Your code actually works for me, but there are a few things you need to be aware off.

As I understand it, Amazon S3 does not have a concept of folders, but individual clients may display the S3 objects as if they did. So if you create an object called A/B , then the client may display it as if it was an object called B inside a folder called A. This is intuitive and seems to have become a standard, but simulating an empty folder does not appear to have a standard.

For example, I used your method to create a folder called Test, then actually end up creating an object called Test/. But I created a folder called Test2 in AWS Explorer (ie the addon to Visual Studio) and it ended up creating an object called Test2/Test2_$folder$ (AWS Explorer will display both Test and Test2 as folders)

Once of the things that this means is that you don't need to create the 'folder' before you can use it, which may mean that you don't need a DoesFolderExist method.

As I mention I tried your code and it works and finds the Test folder it created, but the key had to be tweaked to find the folder created by AWS Explorer , ie

DoesFolderExist("Test/"               , bucketName);  // Returns true
DoesFolderExist("Test2/"              , bucketName);  // Returns false
DoesFolderExist("Test2/Test2_$folder$", bucketName);  // Returns true

So if you do still want to have a DoesFolderExist method, then it might be safer to just look for any objects that start with folderName + "/" , ie something like

ListObjectsRequest request = new ListObjectsRequest();
request.BucketName = bucketName ;
request.WithPrefix(folderName + "/");
request.MaxKeys = 1;

using (ListObjectsResponse response = m_S3Client.ListObjects(request))
{
    return (response.S3Objects.Count > 0);
}



回答2:


        ListObjectsRequest findFolderRequest = new ListObjectsRequest();
        findFolderRequest.BucketName = bucketName;
        findFolderRequest.Prefix = path;
        ListObjectsResponse findFolderResponse = s3Client.ListObjects(findFolderRequest);
        Boolean folderExists = findFolderResponse.S3Objects.Any();

path can be something like "images/40/". Using the above code can check if a so-called folder "images/40/" under bucket exists or not.

But Amazon S3 data model does not have the concept of folders. When you try to copy a image or file to certain path, if this co-called folder does not exist it will be created automatically as part of key name of this file or image. Therefore, you actually do not need to check if this folder exists or not.

Very important information from docs.aws.amazon.com : The Amazon S3 data model is a flat structure: you create a bucket, and the bucket stores objects. There is no hierarchy of subbuckets or subfolders; however, you can infer logical hierarchy using key name prefixes and delimiters as the Amazon S3 console does.

http://docs.aws.amazon.com/AmazonS3/latest/dev/UsingMetadata.html



来源:https://stackoverflow.com/questions/9944671/amazon-s3-creating-folder-through-net-sdk-vs-through-management-console

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!