繁体   English   中英

如何在亚马逊 s3 存储桶中创建子目录

[英]How to create subdirectory in amazon s3 bucket

我正在使用 Amazon S3 Web 服务,但无法在我的存储桶上创建子目录。

我想要这样的东西

当用户上传文件时,会在 S3 Bucket 上创建一个名为 user id 的子目录,文件存储在子目录中。

我正在使用以下代码

 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;
    }

请帮助我实现这一目标。

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);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM