Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

R2 how to implement in .net project? #4683

Closed
OrhanAykac opened this issue Jun 3, 2022 · 4 comments · Fixed by #6317
Closed

R2 how to implement in .net project? #4683

OrhanAykac opened this issue Jun 3, 2022 · 4 comments · Fixed by #6317
Assignees
Labels
content:new Request for new/missing content documentation Documentation edits

Comments

@OrhanAykac
Copy link

Which Cloudflare product(s) does this pertain to?

R2

Subject Matter

.Net Example

Content Location

Hi,
How to use R2 in .net project.
I cant find any document or example anywhere.
Thanks.

Additional information

No response

@OrhanAykac OrhanAykac added content:new Request for new/missing content documentation Documentation edits labels Jun 3, 2022
@deadlypants1973 deadlypants1973 added this to the R2 Documentation milestone Jun 7, 2022
@OrhanAykac
Copy link
Author

Hi, any news?

@KianNH
Copy link
Contributor

KianNH commented Jun 17, 2022

It'll be no different to any other S3-compatible provider. Point it to your endpoint with the proper tokens (https://developers.cloudflare.com/r2/platform/s3-compatibility/tokens/) and make sure the bucket region is auto, us-east-1 or empty.

@vlovich
Copy link
Contributor

vlovich commented Oct 13, 2022

Adding onto this, you can probably use any S3 SDK (e.g. https://aws.amazon.com/sdk-for-net/). We don't yet have examples for C# libraries it looks like. https://docs.aws.amazon.com/sdk-for-net/v3/developer-guide/quick-start-s3-1-cross.html is probably a fine initial guide. Note that you probably want to customize your creds either by adjusting the shared AWS credentials file as explained on that page (~/.aws/credentials on Linux and macOS systems, and %USERPROFILE%\.aws\credentials on Windows) or passing the creds to new AmazonS3Client() (I think the API docs are here https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/S3/TS3Client.html)

@KianNH
Copy link
Contributor

KianNH commented Oct 13, 2022

As an example until I finish it up & PR it to the docs:

private static IAmazonS3 s3Client;

public static void Main(string[] args) {
  var accessKey = "<ACCESS_KEY_ID>";
  var secretKey = "<SECRET_KEY_ID>";
  var credentials = new BasicAWSCredentials(accessKey, secretKey);
  s3Client = new AmazonS3Client(credentials, new AmazonS3Config {
    ServiceURL = "https://<ACCOUNT_ID>.r2.cloudflarestorage.com",
  });

  PutObject().Wait();
}

static async Task PutObject() {
    var request = new PutObjectRequest {
      FilePath = @ "/Users/kian/Downloads/foo.dmg",
        BucketName = "sdk-example",
        DisablePayloadSigning = true // required otherwise it attempts to use streaming sigv4
    };

    var response = await s3Client.PutObjectAsync(request)

    Console.WriteLine(response.ETag);
}    

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
content:new Request for new/missing content documentation Documentation edits
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants