C# 如何在AmazonS3 bucket中创建子目录
我正在使用AmazonS3Web服务,无法在我的bucket上创建子目录 我想要那样的东西 当用户上传文件时,会在S3 Bucket上创建一个名为用户id的子目录,并将文件存储到子目录中 我正在使用以下代码C# 如何在AmazonS3 bucket中创建子目录,c#,amazon-web-services,amazon-s3,C#,Amazon Web Services,Amazon S3,我正在使用AmazonS3Web服务,无法在我的bucket上创建子目录 我想要那样的东西 当用户上传文件时,会在S3 Bucket上创建一个名为用户id的子目录,并将文件存储到子目录中 我正在使用以下代码 AmazonS3Client s3Client = new AmazonS3Client("AWSAccessKey", "AWSSecretKey", Amazon.RegionEndpoint.APSoutheast1); protected void btnUpload
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;
}
AmazonS3Client s3Client=新的AmazonS3Client(“AWSAccessKey”、“AWSSecretKey”、Amazon.RegionEndpoint.APSoutheast1);
受保护的void btnUpload\u单击(对象发送方,事件参数e)
{
如果(!string.IsNullOrEmpty(fileUpload.FileName))
{
//将文件保存到本地磁盘文件夹。
字符串filePath=Server.MapPath(“aa”)+“\\”+fileUpload.FileName;
字符串fileExtension=fileUpload.FileName.Substring(fileUpload.FileName.LastIndexOf(“.”+1);
fileUpload.SaveAs(filePath);
string contentType=GetContentType(文件扩展名);
//将给定对象推入S3桶中
PutObjectRequest objReq=新的PutObjectRequest
{
Key=fileUpload.FileName,
FilePath=FilePath,
ContentType=ContentType,
BucketName=“datastore.MyData”,
CannedACL=S3CannedACL.Private,
};
PutObjectResponse=s3Client.PutObject(objReq);
如果(response.ETag!=null)
{
字符串etag=response.etag;
字符串versionID=response.versionID;
Write(“警报(‘文件成功上传到S3存储桶’);”;
}
//删除本地保存的文件
if(File.Exists(filePath))
{
File.Delete(文件路径);
}
}
}
私有字符串GetContentType(字符串文件扩展名)
{
string contentType=string.Empty;
交换机(文件扩展名)
{
案例“bmp”:contentType=“image/bmp”;中断;
案例“jpeg”:contentType=“image/jpeg”;中断;
案例“jpg”:contentType=“image/jpg”;中断;
案例“gif”:contentType=“image/gif”;中断;
案例“tiff”:contentType=“image/tiff”;中断;
案例“png”:contentType=“image/png”;中断;
大小写“plain”:contentType=“text/plain”;中断;
案例“rtf”:contentType=“text/rtf”;中断;
案例“msword”:contentType=“application/msword”;中断;
案例“zip”:contentType=“application/zip”;中断;
案例“mpeg”:contentType=“音频/mpeg”;中断;
案例“pdf”:contentType=“application/pdf”;中断;
案例“xgzip”:contentType=“application/x-gzip”;中断;
案例“xcompressed”:contentType=“applicationin/x-compressed”;中断;
}
返回contentType;
}
请帮助我实现这一目标 在
s3
中没有文件夹,只有key/value
对。键可以包含斜杠(“/”
,这将使它在管理控制台中显示为文件夹,但从编程角度看,它不是文件夹,而是字符串值
我们使用结尾带有“/”字符的FileKey来表示您想要创建一个文件夹
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,
};
您也可以使用此方法
string S3FileDir = "/Demo/Test/";
TransferUtility fileTransferUtility = new TransferUtility(keyId,secretekey,RegionEndpoint.USWest);
fileTransferUtility.Upload(localFilePath, BucketName + S3FileDir);
最简单的方法是使用TransferUtility
var fileTransferUtility = new TransferUtility(_s3Client);
fileTransferUtility.Upload(localFilePath, bucketName+"/"+ subFolder);