Skip to main content

Download Pre-signed Object

If you want to allow others to receive the object without providing your credential keys, you can use the pre-signed download method.

Components

  • Bucket Name
  • Object Name
  • Link Expiration Time (Optional)

Object Name (Key)

Object name we want to download.

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

using Amazon;
using Amazon.S3;
using Amazon.S3.Model;
using System;
using System.Threading.Tasks;
using System.Reflection;

namespace DownloadPresignedObject
{
class DownloadPresignedObject
{
private static IAmazonS3 _s3Client;

private const string BUCKET_NAME = "<OBJECT_NAME>";
private const string OBJECT_NAME = "<BUCKET_NAME>";

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

GeneratePreSignedUrl();
}

private static void GeneratePreSignedUrl()
{
try
{
// Create the request
var getPreSignedUrlRequest = new GetPreSignedUrlRequest
{
BucketName = BUCKET_NAME,
Key = OBJECT_NAME,
Expires = DateTime.Now.AddHours(1.0),
Verb = HttpVerb.GET
};

// Submit the request
string url = _s3Client.GetPreSignedURL(getPreSignedUrlRequest);
Console.WriteLine($"URL: {url}");
}
catch (AmazonS3Exception amazonS3Exception)
{
Console.WriteLine("An AmazonS3Exception was thrown. Exception: " + amazonS3Exception.ToString());
}
catch (Exception e)
{
Console.WriteLine("Exception: " + e.ToString());
}
}
}
}