Jins Peter
Jins Peter

Reputation: 2469

Upload Image to Amazon S3 bucket, .NETCore2

I want to upload an Image to S3 bucket. Is it an HTTP Request? or Do I have to use AWS toolkit and SDK or something else. The documentation seem to be very tidy/

Upvotes: 1

Views: 2251

Answers (1)

Wah Yuen
Wah Yuen

Reputation: 1619

You can achieve this quite easily using the existing SDKs

AWS and .Net Core 2.0

.NET Core 2.0 supports .NET Standard 2.0 which means any NuGet packages that target .NET Standard 2.0 and below are supported on .NET Core 2.0. The AWS SDK for .NET targets .NET Standard 1.3 which means you can use it for either .NET Core 1.x or .NET Core 2.0.

You can import the AWSSDK.S3 nuget package which contains the libraries for interacting with an S3 Bucket.

Example:

    // if you happened to store this in your appsettings.json
    var accessKey = _configuration["AwsAccessKey"];
    var secretKey = _configuration["AwsSecretKey"];

    var client = new AmazonS3Client(accessKey, secretKey, Amazon.RegionEndpoint.USEast1);

    var request = new PutObjectRequest
    {
        BucketName = "BucketName",
        Key = "KeyName",
        FilePath = "FilePath"
    };

    var response = client.PutObjectAsync(request).GetAwaiter().GetResult();

For further details and examples, please see Upload an Object Using the AWS SDK for .NET

Update - modified to show example of passing in AccessKey and SecretKey while creating the client

Upvotes: 5

Related Questions