Amazon S3 Creating a folder through the .NET SDK through the management console

I am trying to determine if a folder exists in my Amazon S3 carpet, and if it is not, I want to create it.

Currently, I can create a 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 a folder exists with 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; } } 

He cannot find the folder. The strange thing is, if I create a folder using the AWS management console, the "DoesFolderExist" method can see it.

I'm not sure if this is an ACL / IAM, but I'm not sure how to allow this.

+7
source share
2 answers

Your code really works for me, but there are a few things you need to know.

As I understand it, Amazon S3 does not have the concept of folders, but individual clients can display S3 objects as if they were doing it. Therefore, if you create an object named A / B, then the client can display it as if it were object B inside a folder named A. This is intuitive and seems to have become 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, and then actually created an object called Test /. But I created the Test2 folder in AWS Explorer (that is, an addon to Visual Studio), and in the end she created an object called Test2 / Test2_ $ folder $ (AWS Explorer will display both Test and Test2 as folders)

Once out of what this means is that you do not need to create a โ€œfolderโ€ before you can use it, which may mean that you do not need the DoFolderExist method.

As I mentioned, I tried my code and it works and finds the created Test folder, but the key had to be changed to find the folder created by AWS Explorer, i.e.

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

So, if you still want to have a DoFolderExist method, then it might be safer to just look for any objects that start with folderName + "/", that is, 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); } 
+10
source
  ListObjectsRequest findFolderRequest = new ListObjectsRequest(); findFolderRequest.BucketName = bucketName; findFolderRequest.Prefix = path; ListObjectsResponse findFolderResponse = s3Client.ListObjects(findFolderRequest); Boolean folderExists = findFolderResponse.S3Objects.Any(); 

the path may be something like "images / 40 /". Using the code above, you can check if the so-called "images / 40 /" folder exists under the bucket.

But the Amazon S3 data model has no concept of folders. When you try to copy an image or file to a specific path, if this shared folder does not exist, it will be created automatically as part of the key name of this file or image. Therefore, you really do not need to check if this folder exists.

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. No hierarchy of subpackages or subfolders; however, you can infer a logical hierarchy using the name prefix and key separators, as the Amazon S3 console does.

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

0
source

All Articles