Problem uploading files to cloudflare r2 with ASP.NET Core Web API

286 views Asked by At

I want to make a small API, but I could not solve the problem.

I created a simple ASP.NET Core Web API project. I'm trying to upload files to cloudflare r2 using AWSSDK but I'm getting an error.

Code:

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

    }
}

1st problem in this code:

"STREAMING-AWS4-HMAC-SHA256-PAYLOAD not implemented"

To fix this issue I am editing the following code:

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

This time I get another error:

{Please use "STREAMING-AWS4-HMAC-SHA256-PAYLOAD"}

I couldn't solve the problem. Anyone have an idea or an alternative solution?

0

There are 0 answers