Amazon S3通过.NET SDK与通过管理控制台创建文件夹 [英] Amazon S3 Creating Folder through .NET SDK vs through Management Console

查看:286
本文介绍了Amazon S3通过.NET SDK与通过管理控制台创建文件夹的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试确定我的Amazon S3存储桶上是否存在文件夹,如果我不想创建该文件夹。

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

目前,我可以使用.NET SDK创建文件夹,如下所示:

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;
        }
    }

找不到文件夹。奇怪的是,如果我使用AWS管理控制台创建文件夹,则'DoesFolderExist'方法可以看到它。

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.

我不确定这是否是ACL / IAM事情,但不确定如何解决。

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

推荐答案

您的代码实际上对我有用,但是您需要一些注意事项

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

据我了解,Amazon S3没有文件夹的概念,但单个客户端可能会像显示它们一样显示S3对象。因此,如果您创建一个名为A / B的对象,则客户端可能会在一个名为A的文件夹中将其显示为好像是一个名为B的对象。这很直观,似乎已经成为标准,但不会出现模拟空文件夹的情况。

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.

例如,我使用您的方法创建了一个名为Test的文件夹,然后最终创建了一个名为Test /的对象。但是我在AWS Explorer中创建了一个名为Test2的文件夹(即Visual Studio的插件),最终创建了一个名为Test2 / Test2_ $ folder $
的对象(AWS Explorer将Test和Test2都显示为文件夹)

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)

一旦出现这意味着您无需 就可以创建文件夹,就可以使用它了。您不需要DoesFolderExist方法。

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.

正如我提到的,我尝试了您的代码,它可以工作并找到它创建的Test文件夹,但是必须对键进行调整才能找到由AWS Explorer创建的文件夹,即

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

因此,如果您仍然希望拥有DidFolderExist方法,则可能会更安全只寻找以folderName + /开头的任何对象,例如

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);
}

这篇关于Amazon S3通过.NET SDK与通过管理控制台创建文件夹的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆