پرش به مطلب اصلی

بارگذاری Pre-signed داخل صندوقچه

اگر بخواهید بدون انتشار کلیدهای احراز هویت‌ خود، امکان بارگذاری آبجکت را در اختیار دیگران قرار دهید، در این شرایط می‌توانید از روش بارگذاری pre-signed استفاده کنید.

مولفه‌ها

نام آبجکت (Key)

نام آبجکت پس از بارگذاری داخل صندوقچه

// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: Apache-2.0

namespace GenPresignedURLExample
{
using System;
using Amazon;
using Amazon.S3;
using Amazon.S3.Model;

/// <summary>
/// This example generates a presigned URL for an object in an Amazon
/// Simple Storage Service (Amazon S3) bucket. The generated example
/// 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.
/// </summary>
public class GenPresignedURL
{
private const string OBJECT_NAME = "<BUCKET_NAME>";
private static string LOCAL_PATH = Environment.GetFolderPath(Environment.SpecialFolder.MyDocuments);

public static void Main()
{
const string bucketName = "<OBJECT_NAME>";
var path = $"{LOCAL_PATH}/{OBJECT_NAME}";
var objectKey = path;

// Specify how long the presigned URL lasts, in hours
const double timeoutDuration = 12;

// Specify the AWS Region of your Amazon S3 bucket. An example AWS
// Region is shown.
RegionEndpoint bucketRegion = RegionEndpoint.USWest2;

var awsCredentials = new Amazon.Runtime.BasicAWSCredentials("<ACCESS-KEY>", "<SECRET-KEY>");
var config = new AmazonS3Config { ServiceURL = "<ENDPOINT>" };
IAmazonS3 _s3Client = new AmazonS3Client(awsCredentials, config);

string urlString = GeneratePresignedURL(_s3Client, bucketName, objectKey, timeoutDuration);
Console.WriteLine($"The generated URL is: {urlString}");
}

/// <summary>
/// Gemerate a presigned URL that can be used to access the file named
/// in the ojbectKey parameter for the amount of time specified in the
/// duration parameter.
/// </summary>
/// <param name="client">An initialized S3 client object used to call
/// the GetPresignedUrl method.</param>
/// <param name="bucketName">The name of the S3 bucket containing the
/// object for which to create the presigned URL.</param>
/// <param name="objectKey">The name of the object to access with the
/// presigned URL.</param>
/// <param name="duration">The length of time for which the presigned
/// URL will be valid.</param>
/// <returns>A string representing the generated presigned URL.</returns>
public static string GeneratePresignedURL(IAmazonS3 client, string bucketName, string objectKey, double duration)
{
string urlString = string.Empty;
try
{
GetPreSignedUrlRequest request = new ()
{
BucketName = bucketName,
Key = objectKey,
Expires = DateTime.UtcNow.AddHours(duration),
};
urlString = client.GetPreSignedURL(request);
}
catch (AmazonS3Exception ex)
{
Console.WriteLine($"Error:'{ex.Message}'");
}

return urlString;
}
}
}