.NET용 AWS SDK를 사용하여 미리 서명된 객체 URL 생성
다음 작업은 .NET 클래스를 사용하여 미리 서명된 URL을 만드는 방법을 보여줍니다.
객체 다운로드
1 |
|
2 |
버킷 이름, 객체 키 및 만료 날짜 등의 정보를 제공하여 |
다음은 위에서 설명한 작업을 실행하는 C# 코드 예제입니다.
static IAmazonS3 s3Client; s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1) GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest() { BucketName = bucketName, Key = objectKey, Expires = DateTime.Now.AddMinutes(5) }; string url = s3Client.GetPreSignedURL(request1);
예
다음 C# 코드 예는 특정 객체의 미리 서명된 URL을 만듭니다. 실제 예제를 작성하여 테스트하는 방법에 대한 자세한 내용은 Amazon S3 .NET 코드 예시 실행를 참조하십시오.
using System; using Amazon.S3; using Amazon.S3.Model; namespace s3.amazon.com.docsamples { class GeneratePresignedURL { static string bucketName ="*** Provide a bucket name ***"; static string objectKey = "*** Provide an object name ***"; static IAmazonS3 s3Client; public static void Main(string[] args) { using (s3Client = new AmazonS3Client(Amazon.RegionEndpoint.USEast1)) { string urlString = GeneratePreSignedURL(); } Console.WriteLine("Press any key to continue..."); Console.ReadKey(); } static string GeneratePreSignedURL() { string urlString = ""; GetPreSignedUrlRequest request1 = new GetPreSignedUrlRequest { BucketName = bucketName, Key = objectKey, Expires = DateTime.Now.AddMinutes(5) }; try { urlString = s3Client.GetPreSignedURL(request1); //string url = s3Client.GetPreSignedURL(request1); } catch (AmazonS3Exception amazonS3Exception) { if (amazonS3Exception.ErrorCode != null && (amazonS3Exception.ErrorCode.Equals("InvalidAccessKeyId") || amazonS3Exception.ErrorCode.Equals("InvalidSecurity"))) { Console.WriteLine("Check the provided AWS Credentials."); Console.WriteLine( "To sign up for service, go to http://aws.amazon.com/s3"); } else { Console.WriteLine( "Error occurred. Message:'{0}' when listing objects", amazonS3Exception.Message); } } catch (Exception e) { Console.WriteLine(e.Message); } return urlString; } } }