Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 11 additions & 7 deletions dotnetv3/S3/GenPresignedURLExample/GenPresignedUrl.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,7 @@
/// <summary>
/// This example generates a presigned URL for an object in an Amazon
/// Simple Storage Service (Amazon S3) bucket. The generated URL
/// remains valid for the specified number of hours. This example was
/// created using the AWS SDK for .NET version 3.7 and .NET Core 5.0.
/// remains valid for the specified number of hours.
/// </summary>
namespace GenPresignedUrlExample
{
Expand All @@ -25,12 +24,17 @@ public static void Main()
// Specify how long the presigned URL lasts, in hours
const double timeoutDuration = 12;

// Specify the AWS Region of your Amazon S3 bucket if it is
// Specify the AWS Region of your Amazon S3 bucket. If it is
// different from the Region defined for the default user,
// pass the Region to the constructor for the client. For
// example:
// RegionEndpoint bucketRegion = RegionEndpoint.USWest2;
IAmazonS3 s3Client = new AmazonS3Client();
// example: new AmazonS3Client(RegionEndpoint.USEast1);

// If using the Region us-east-1, and server-side encryption with AWS KMS, you must specify Signature Version 4.
// Region us-east-1 defaults to Signature Version 2 unless explicitly set to Version 4 as shown below.
// For more details, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAWSSDK.html#specify-signature-version
// and https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Amazon/TAWSConfigsS3.html
AWSConfigsS3.UseSignatureVersion4 = true;
IAmazonS3 s3Client = new AmazonS3Client(RegionEndpoint.USEast1);

string urlString = GeneratePresignedURL(s3Client, bucketName, objectKey, timeoutDuration);
Console.WriteLine($"The generated URL is: {urlString}.");
Expand Down Expand Up @@ -73,4 +77,4 @@ public static string GeneratePresignedURL(IAmazonS3 client, string bucketName, s
}

// snippet-end:[S3.dotnetv3.GenPresignedUrlExample]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.3.34" />
<PackageReference Include="AWSSDK.S3" Version="3.7.4.1" />
<PackageReference Include="AWSSDK.Core" Version="3.7.201.3" />
<PackageReference Include="AWSSDK.S3" Version="3.7.202.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@
/// This example shows how to upload an object to an Amazon Simple Storage
/// Service (Amazon S3) bucket using a presigned URL. The code first
/// creates a presigned URL and then uses it to upload an object to an
/// Amazon S3 bucket using that URL. The example was created using the
/// AWS SDK for .NET version 3.7 and .NET Core 5.0.
/// Amazon S3 bucket using that URL.
/// </summary>
namespace UploadUsingPresignedURLExample
{
Expand All @@ -15,6 +14,7 @@ namespace UploadUsingPresignedURLExample
using System.IO;
using System.Net;
using System.Threading.Tasks;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

Expand All @@ -29,11 +29,17 @@ public static void Main()
// Specify how long the signed URL will be valid in hours.
double timeoutDuration = 12;

// If the AWS Region defined for your default user is different
// from the Region where your Amazon S3 bucket is located,
// pass the Region name to the Amazon S3 client object's constructor.
// For example: RegionEndpoint.USWest2.
IAmazonS3 client = new AmazonS3Client();
// Specify the AWS Region of your Amazon S3 bucket. If it is
// different from the Region defined for the default user,
// pass the Region to the constructor for the client. For
// example: new AmazonS3Client(RegionEndpoint.USEast1);

// If using the Region us-east-1, and server-side encryption with AWS KMS, you must specify Signature Version 4.
// Region us-east-1 defaults to Signature Version 2 unless explicitly set to Version 4 as shown below.
// For more details, see https://docs.aws.amazon.com/AmazonS3/latest/userguide/UsingAWSSDK.html#specify-signature-version
// and https://docs.aws.amazon.com/sdkfornet/v3/apidocs/items/Amazon/TAWSConfigsS3.html
AWSConfigsS3.UseSignatureVersion4 = true;
IAmazonS3 client = new AmazonS3Client(RegionEndpoint.USEast1);

var url = GeneratePreSignedURL(client, bucketName, keyName, timeoutDuration);
var success = UploadObject(filePath, url);
Expand Down Expand Up @@ -111,4 +117,4 @@ public static string GeneratePreSignedURL(
}

// snippet-end:[S3.dotnetv3.UploadUsingPresignedURLExample]
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,12 @@

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net5.0</TargetFramework>
<TargetFramework>net6.0</TargetFramework>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="AWSSDK.Core" Version="3.7.4.1" />
<PackageReference Include="AWSSDK.S3" Version="3.7.4.3" />
<PackageReference Include="AWSSDK.Core" Version="3.7.201.3" />
<PackageReference Include="AWSSDK.S3" Version="3.7.202.3" />
<PackageReference Include="StyleCop.Analyzers" Version="1.1.118">
<IncludeAssets>runtime; build; native; contentfiles; analyzers; buildtransitive</IncludeAssets>
<PrivateAssets>all</PrivateAssets>
Expand Down