如何在Amazon S3中创建文件夹

时间:2013-12-19 00:29:46

标签: c# amazon-s3 amazon

下面是我将文档上传到S3的代码,但是如果它不存在,我可以帮助如何在存储桶中创建文件夹并将文件上传到该文件夹​​

try
        {
            TransferUtility fileTransferUtility = new
            TransferUtility(new AmazonS3Client(Amazon.RegionEndpoint.USEast1));

            // Specify advanced settings/options.
            TransferUtilityUploadRequest fileTransferUtilityRequest = new TransferUtilityUploadRequest
            {
                BucketName = existingBucketName,
                FilePath = filePath,
                StorageClass = S3StorageClass.ReducedRedundancy,
                PartSize = 9291456, // 6 MB.
                Key = keyName,
                CannedACL = S3CannedACL.PublicRead
            };
            fileTransferUtility.UploadDirectory("C:\\Build", existingBucketName);
            fileTransferUtilityRequest.Metadata.Add("param1", "Value1");
            fileTransferUtilityRequest.Metadata.Add("param2", "Value2");
            fileTransferUtility.Upload(fileTransferUtilityRequest);
            Console.WriteLine("Upload 4 completed");

        }
        catch (AmazonS3Exception s3Exception)
        {
            Console.WriteLine(s3Exception.Message,
                              s3Exception.InnerException);
        }

2 个答案:

答案 0 :(得分:2)

重要的是要知道Amazon S3上没有文件夹的概念。这些只是长S3文件名,其中包含斜杠,用于分组Amazon S3 Objects ..

通过发送PUT对象请求,您可以创建一个文件夹。在请求中,您需要在其名称中加上“/”,然后用“/”分隔的字符串和文件大小为零。

有关详情:Amazon S3 - How to Create a Folder?

答案 1 :(得分:1)

public void CreateFolder(string awsBucketName, string awsFolderName)
{
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(RSA.Create());

    AmazonS3EncryptionClient client = new AmazonS3EncryptionClient(encryptionMaterials);
        if ((cloudKaseClient != null) & (_Security.IsTokenAuthenticate(tokenUsr, tokenPasswd)))
        {
            PutObjectRequest putObjectRequest = new PutObjectRequest
            {

                BucketName = awsBucketName,
                StorageClass = S3StorageClass.Standard,
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
                CannedACL = S3CannedACL.Private,
                Key = awsFolderName + "/",
                ContentBody = awsFolderName
            };

            client.PutObject(putObjectRequest);

    }
}

对于Amazon S3的概念,如果您想区分文件和文件夹而不是文件夹路径,请使用“/”获取路径,而使用文件路径获取“/”。

此外,如果你想用“/”创建文件夹名称而不是亚马逊s3浏览器创建没有“/".

的文件夹

创建文件使用下面的代码

public void CreateFile(string awsBucketName, string awsFolderName)
{
    EncryptionMaterials encryptionMaterials = new EncryptionMaterials(RSA.Create());

    AmazonS3EncryptionClient client = new AmazonS3EncryptionClient(encryptionMaterials);
        if ((cloudKaseClient != null) & (_Security.IsTokenAuthenticate(tokenUsr, tokenPasswd)))
        {
            PutObjectRequest putObjectRequest = new PutObjectRequest
            {

                BucketName = awsBucketName,
                StorageClass = S3StorageClass.Standard,
                ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256,
                CannedACL = S3CannedACL.Private,
                Key = awsFolderName,
                ContentBody = awsFolderName
            };

            client.PutObject(putObjectRequest);

    }
}