【问题标题】:.NET Core Image not stored in s3 bucket.NET Core 映像未存储在 s3 存储桶中
【发布时间】:2021-08-09 15:23:59
【问题描述】:

我的函数已成功执行,但图像未存储在 s3 存储桶中。我需要你的帮助来了解我哪里做错了

public IActionResult load([FromForm(Name = "file")] IFormFile file)
{
    var credentials = new EnvironmentVariablesAWSCredentials();
    var client = new AmazonS3Client(credentials, RegionEndpoint.USEast1);

    PutObjectRequest request = new PutObjectRequest
    {
        BucketName = "#######",
        Key = file.FileName,
        InputStream = file.OpenReadStream(),
        ContentType = file.ContentType
    };

    var result = client.PutObjectAsync(request);
    return Ok(result);
}

【问题讨论】:

  • 您的PutObjectRequest 成员被设置为什么值(您可以共享的值)?我记得Key 成员对命名有点讲究。
  • 关键成员正在获取参数文件传递的确切文件名。

标签: c# amazon-web-services amazon-s3 .net-core


【解决方案1】:

试试这个确切的 .NET 代码:

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;

namespace UploadObject
{
    // The following example shows two different methods for uploading data to
    // an Amazon Simple Storage Service (Amazon S3) bucket. The method,
    // UploadObjectFromFileAsync, uploads an existing file to an Amazon S3
    // bucket. The method, UploadObjectFromContentAsync, creates a new
    // file containing the text supplied to the method. The application
    // was created using AWS SDK for .NET 3.5 and .NET 5.0.

    class UploadObject
    {
        private static IAmazonS3 _s3Client;

        private const string BUCKET_NAME = "doc-example-bucket";
        private const string OBJECT_NAME1 = "objectname1.txt";
        private const string OBJECT_NAME2 = "objectname2.txt";

        private static string LOCAL_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

        static async Task Main()
        {
            _s3Client = new AmazonS3Client();

            // The method expects the full path, including the file name.
            var path = $"{LOCAL_PATH}\\{OBJECT_NAME1}";

            await UploadObjectFromFileAsync(_s3Client, BUCKET_NAME, OBJECT_NAME1, path);
            await UploadObjectFromContentAsync(_s3Client, BUCKET_NAME, OBJECT_NAME2, "This is a test...");
        }

        /// <summary>
        /// This method uploads a file to an Amazon S3 bucket. This
        /// example method also adds metadata for the uploaded file.
        /// </summary>
        /// <param name="client">An initialized Amazon S3 client object.</param>
        /// <param name="bucketName">The name of the S3 bucket to upload the
        /// file to.</param>
        /// <param name="objectName">The destination file name.</param>
        /// <param name="filePath">The full path, including file name, to the
        /// file to upload. This doesn't necessarily have to be the same as the
        /// name of the destination file.</param>
        private static async Task UploadObjectFromFileAsync(
            IAmazonS3 client,
            string bucketName,
            string objectName,
            string filePath)
        {
            try
            {
                var putRequest = new PutObjectRequest
                {
                    BucketName = bucketName,
                    Key = objectName,
                    FilePath = filePath,
                    ContentType = "text/plain"
                };

                putRequest.Metadata.Add("x-amz-meta-title", "someTitle");

                PutObjectResponse response = await client.PutObjectAsync(putRequest);
            }
            catch (AmazonS3Exception e)
            {
                Console.WriteLine($"Error: {e.Message}");
            }
        }

        /// <summary>
        /// This method creates a file in an Amazon S3 bucket that contains the text
        /// passed to the method.
        /// </summary>
        /// <param name="client">An initialized S3 client object.</param>
        /// <param name="bucketName">The name of the bucket where the file will
        /// be created.</param>
        /// <param name="objectName">The name of the file that will be created.</param>
        /// <param name="content">A string containing the content to put in the
        /// file on the destination S3 bucket.</param>
        private static async Task UploadObjectFromContentAsync(IAmazonS3 client,
            string bucketName,
            string objectName,
            string content)
        {
            var putRequest = new PutObjectRequest
            {
                BucketName = bucketName,
                Key = objectName,
                ContentBody = content
            };

            PutObjectResponse response = await client.PutObjectAsync(putRequest);
        }
    }
}

【讨论】:

  • 谢谢你的解决方案......在这里,我只想说,因为我们使用的是“IAmazon客户端”,所以必须处理依赖注入......在启动文件中
猜你喜欢
  • 1970-01-01
  • 2018-12-19
  • 1970-01-01
  • 1970-01-01
  • 2021-11-14
  • 1970-01-01
  • 2019-05-10
  • 2013-12-21
  • 1970-01-01
相关资源
最近更新 更多