Search code examples
asp.netamazon-s3privateaws-sdkbucket

AWS S3 - access private file from URL


I am using ASP.NET AWS SDK to upload files to S3 bucket from my web application.

public static void UploadFile(string filePath)
{
        using (_client = Amazon.AWSClientFactory.CreateAmazonS3Client(_awsAccessKey, _awsSecretKey, Amazon.RegionEndpoint.USEast1))
        {
            var request = new PutObjectRequest()
            {
                Key = String.Format("{0}{1}", Guid.NewGuid(), Path.GetExtension(filePath)),
                BucketName = _bucketName,
                CannedACL = S3CannedACL.Private,               
                FilePath = filePath
            };

            _client.PutObject(request);
        }
}

Since it is a private file (CannedACL is Private), I cannot access the file from URL. (https://s3.amazonaws.com/mybucketname/image.png)

But I want to access the file from url ONLY if I am logged to the asp.net web application that I developed.

Is there a way to do this?


Solution

  • To access a 'private' file, you either need to use an API call that includes authentication information or you could use a Pre-Signed URL.

    A Pre-signed URL is a time-limited URL that includes an encrypted authorization string, permitting access to a private object.

    The pre-signed URL can be generated by a few lines of code and requires access credentials of an IAM User that has permission to access the private file.

    See documentation: Generate a Pre-signed Object URL using AWS SDK for .NET