使用 ASP.NET Core Web API 将文件上传到 cloudflare r2 时出现问题

问题描述 投票:0回答:1

我想做一个小型API,但无法解决问题。

我创建了一个简单的 ASP.NET Core Web API 项目。我正在尝试使用 AWSSDK 将文件上传到 cloudflare r2,但出现错误。

代码:

using Amazon.Runtime;
using Amazon.S3;
using Amazon.S3.Model;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;
using Microsoft.AspNetCore.Mvc.Infrastructure;

namespace xxx.API.Controllers
{
    [ApiController]
    [Route("api/[controller]")]
    [AllowAnonymous]
    public class CloudflareR2Controller
    {
        private readonly IAmazonS3 _s3Client;

        public CloudflareR2Controller(IConfiguration configuration)
        {
            string accessKeyId = "aaa";
            string secretAccessKey = "bbb";
            string serviceUrl = "https://ccc.r2.cloudflarestorage.com";
            AWSCredentials credentials = new BasicAWSCredentials(accessKeyId, secretAccessKey);
            AmazonS3Config config = new AmazonS3Config
            {
                ServiceURL = serviceUrl

            };
            _s3Client = new AmazonS3Client(credentials, config);
        }

        [HttpPut("objects/{bucketName}")]
        public async Task<IActionResult> UploadObject(string bucketName, [FromBody] string filePath)
        {
            try
            {
                PutObjectRequest request = new PutObjectRequest
                {
                    FilePath = @filePath,
                    BucketName = bucketName,
                    ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256
                };

                PutObjectResponse response = await _s3Client.PutObjectAsync(request);

                Console.WriteLine("ETag: {0}", response.ETag);
                //await _cloudflareR2Client.UploadObject(filePath, bucketName);
                return Ok(response);
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Error: " + ex.Message);
            }
        }

        private IActionResult StatusCode(int v1, string v2)
        {
            throw new NotImplementedException();
        }

        [HttpGet]
        public virtual OkObjectResult Ok([ActionResultObjectValue] object? value)
        => new OkObjectResult(value);

        [HttpGet("presigned-url/{bucketName}/{objectKey}")]
        public IActionResult GeneratePresignedUrl(string bucketName, string objectKey)
        {
            try
            {
                //_cloudflareR2Client.GeneratePresignedUrl(bucketName, objectKey);
                return Ok(bucketName);
            }
            catch (Exception ex)
            {
                return StatusCode(500, "Error: " + ex.Message);
            }
        }

    }
}

此代码中的第一个问题:

“STREAMING-AWS4-HMAC-SHA256-PAYLOAD 未实现”

为了解决此问题,我正在编辑以下代码:

PutObjectRequest request = new PutObjectRequest
                 {
                     FilePath = @filePath,
                     bucketName = bucketName,
                     DisablePayloadSigning=true, //new code
                     ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256
                 };

这次我又遇到了错误:

{请使用“STREAMING-AWS4-HMAC-SHA256-PAYLOAD”}

我无法解决问题。有人有想法或替代解决方案吗?

asp.net-core-webapi aws-sdk cloudflare cloudflare-r2
1个回答
0
投票

您收到此错误消息的原因是 Cloudflare 不支持 Streaming SigV4。 您需要删除

ServerSideEncryptionMethod = ServerSideEncryptionMethod.AES256
并保留
DisablePayloadSigning=true

您可以参考此页面Cloudflare R2 Asp.net Examples

© www.soinside.com 2019 - 2024. All rights reserved.