如何在 Amazon S3 中创建文件夹
本文关键字:创建 文件夹 S3 Amazon | 更新日期: 2024-10-30 20:57:59
下面是我将文档上传到 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);
}
}
重要的是要知道 Amazon S3 中没有文件夹的概念。这些只是带有斜杠的长 S3 文件名,用于对 Amazon S3 对象进行分组。
通过发送 PUT 对象请求,您可以创建一个文件夹。在请求中,您需要在其名称中输入"/",然后用"/"分隔字符串,文件大小为零。
有关更多详细信息 : Amazon S3 - 如何创建文件夹?