如何在Amazon S3存储桶中创建子目录 [英] How to create subdirectory in amazon s3 bucket
问题描述
我正在使用Amazon S3 Web服务,但无法在我的存储桶上创建子目录.
i am using Amazon S3 Web-services and not able to create sub directory on my bucket.
我想要类似的东西
当用户上传文件时,会在S3 Bucket上创建一个名为user id的子目录,并将文件存储在该子目录中.
when user upload the files, a sub directory create on S3 Bucket with the name of user id and files are stored into the sub directory .
我正在使用以下代码
AmazonS3Client s3Client = new AmazonS3Client("AWSAccessKey", "AWSSecretKey", Amazon.RegionEndpoint.APSoutheast1);
protected void btnUpload_Click(object sender, EventArgs e)
{
if (!string.IsNullOrEmpty(fileUpload.FileName))
{
//Saving File to local disk folder.
string filePath = Server.MapPath("aa") + "\\" + fileUpload.FileName;
string fileExtension = fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf(".") + 1);
fileUpload.SaveAs(filePath);
string contentType = GetContentType(fileExtension);
//Push the given object into S3 Bucket
PutObjectRequest objReq = new PutObjectRequest
{
Key = fileUpload.FileName,
FilePath = filePath,
ContentType = contentType,
BucketName = "datastore.MyData",
CannedACL = S3CannedACL.Private,
};
PutObjectResponse response = s3Client.PutObject(objReq);
if (response.ETag != null)
{
string etag = response.ETag;
string versionID = response.VersionId;
Response.Write("<script>alert('File uploaded to S3 Bucket Successfully.');</script>");
}
//Deleting Localy Saved File
if (File.Exists(filePath))
{
File.Delete(filePath);
}
}
}
private string GetContentType(string fileExtension)
{
string contentType = string.Empty;
switch (fileExtension)
{
case "bmp": contentType = "image/bmp"; break;
case "jpeg": contentType = "image/jpeg"; break;
case "jpg": contentType = "image/jpg"; break;
case "gif": contentType = "image/gif"; break;
case "tiff": contentType = "image/tiff"; break;
case "png": contentType = "image/png"; break;
case "plain": contentType = "text/plain"; break;
case "rtf": contentType = "text/rtf"; break;
case "msword": contentType = "application/msword"; break;
case "zip": contentType = "application/zip"; break;
case "mpeg": contentType = "audio/mpeg"; break;
case "pdf": contentType = "application/pdf"; break;
case "xgzip": contentType = "application/x-gzip"; break;
case "xcompressed": contentType = "applicatoin/x-compressed"; break;
}
return contentType;
}
请帮助我实现这一目标.
Please help me to achieve this.
推荐答案
s3
中没有文件夹,只有key/value
对.密钥可以包含斜杠("/")
,这将使其在管理控制台中显示为文件夹,但是以编程方式,它不是文件夹,而是String值.
There are no folders in s3
, only key/value
pairs. The key can contain slashes ("/")
and that will make it appear as a folder in management console, but programmatically it's not a folder it is a String value.
我们使用FileKey末尾带有字符"/"的字母表示您要创建文件夹.
we use the FileKey with the character '/' at the end to show that you want to create a folder.
PutObjectRequest objReq = new PutObjectRequest
{
Key = "Name of the folder you want to create/" fileUpload.FileName,
FilePath = filePath,
ContentType = contentType,
BucketName = "datastore.MyData",
CannedACL = S3CannedACL.Private,
};
这篇关于如何在Amazon S3存储桶中创建子目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!