gpt4 book ai didi

c# - 如何在 Amazon S3 中创建文件夹

转载 作者:行者123 更新时间:2023-12-02 17:41:03 27 4
gpt4 key购买 nike

下面是我将文档上传到 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);
}

最佳答案

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 的概念,如果您想要区分文件和文件夹,而不是通过带“/”的路径获取文件夹路径,而对于不带“/”的文件路径获取。

此外,如果您想创建带有“/”的文件夹名称,而不是 amazon 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);

}
}

关于c# - 如何在 Amazon S3 中创建文件夹,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20671345/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com