【问题标题】:How to upload file in AWS S3 Bucket through endpoint in ASP.NET Core如何通过 ASP.NET Core 中的端点在 AWS S3 存储桶中上传文件
【发布时间】:2019-12-18 17:09:59
【问题描述】:

我正在使用 ASP.NET Core 和 AWSSDK.S3 nuget 包。

我可以通过提供 accessKeyIDsecretKeybucketNameregion

来上传文件

像这样:

var credentials = new BasicAWSCredentials(accessKeyID, secretKey);

   using (var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1))
   {
       var request = new PutObjectRequest
       {
             AutoCloseStream = true,
             BucketName = bucketName,
             InputStream = storageStream,
             Key = fileName
       }
   }

但我只有一个 URL 来上传文件

11.11.11.111:/aa-bb-cc-dd-useast1

如何通过 URL 上传文件?我是 AWS 新手,如果能得到一些帮助,我将不胜感激。

【问题讨论】:

  • 看起来像问same question..
  • @TuanVA 不完全是,我将从应用程序本身上传文件。我需要知道如何将 AWS VPC enpoint 用于 S3
  • 只需启用 S3 Endpoint,然后通过网络完成剩下的工作。
  • 我需要在 .net core 中找到一些代码。端点已启用。我不确定如何使用 AWSSDK.S3 nuget 包编写代码

标签: amazon-web-services asp.net-core amazon-s3


【解决方案1】:
using Amazon.S3;
using Amazon.S3.Transfer;
using System;
using System.IO;
using System.Threading.Tasks;

namespace Amazon.DocSamples.S3
{
    class UploadFileMPUHighLevelAPITest
    {
       private const string bucketName = "*** provide bucket name ***";
       private const string filePath = "*** provide the full path name of the file to upload ***";
        // Specify your bucket region (an example region is shown).
        private static readonly RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
        private static IAmazonS3 s3Client;

        public static void Main()
        {
            s3Client = new AmazonS3Client(bucketRegion);
            UploadFileAsync().Wait();
        }

        private static async Task UploadFileAsync()
        {
            try
            {
                var fileTransferUtility =
                    new TransferUtility(s3Client);  
                // Option 1. Upload a file. The file name is used as the object key name.
               await fileTransferUtility.UploadAsync(filePath, bucketName);
               Console.WriteLine("Upload 1 completed");

             }
         }
      }
    }

https://docs.aws.amazon.com/AmazonS3/latest/dev/HLuploadFileDotNet.html

您可以使用提供的访问点代替存储桶名称。

https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TPutObjectRequest.html

【讨论】:

  • 如果给你一个 url,那么你猜那是存储桶的访问点。将存储桶名称替换为访问点。
  • 好的。谢谢你。我会尽力让你知道
  • 嗨,我收到错误消息:“无法从 EC2 实例元数据服务获取 IAM 安全凭证。”我认为如果我获得访问权限,这应该可以。有什么想法吗?
  • 是的。该应用程序应在 Web 配置或代码中具有 aws 帐户的凭据
  • 谢谢。我是 AWS 新手,知识很少。
猜你喜欢
  • 2017-09-17
  • 2018-04-25
  • 2022-01-27
  • 2015-07-23
  • 1970-01-01
  • 2021-09-27
  • 1970-01-01
  • 1970-01-01
  • 2021-03-06
相关资源
最近更新 更多