如何在Amazon S3存储桶中创建子目录

 手机用户2602883115 发布于 2022-12-10 17:22

我正在使用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("");
            }
            //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;
    }

请帮助我实现这一目标。

1 个回答
  • 中没有文件夹s3,只有key/value成对的文件夹。该键可以包含斜杠("/"),这会使它在管理控制台中显示为文件夹,但是以编程方式,它不是一个文件夹,而是一个String值。

    我们在文件尾使用带字符'/'的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,               
    
                };
    

    2022-12-11 02:06 回答
撰写答案
今天,你开发时遇到什么问题呢?
立即提问
热门标签
PHP1.CN | 中国最专业的PHP中文社区 | PNG素材下载 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有