3

I have a bucket my-bucket-name and I want to grant temporary access to some file.pdf in folder-name. As for default I get next link using boto3:

https://my-bucket-name.s3.amazonaws.com/folder-name/file.pdf?AWSAccessKeyId=<key>&Signature=<signature>&x-amz-security-token=<toke>&Expires=<time>

But also I've got a DNS alias, my.address.com is mapped to my-bucket-name.s3.amazonaws.com. Of course, if I'm using it directly I got SignatureDoesNotMatch from amazon. So I'm using next code to generate pre-signed link:

from botocore.client import Config

kwargs = {}
kwargs['endpoint_url'] = f'https://my.address.com'
kwargs['config'] = Config(s3={'addressing_style': 'path'})

s3_client = boto3.client('s3', **kwargs)
url = s3_client.generate_presigned_url(ClientMethod='get_object',
                                       Params={
                                          'Bucket': 'my-bucket-name',
                                          'Key': 'folder-name/file.pdf'
                                       },
                                       ExpiresIn=URL_EXPIRATION_TIME)

As a result it returns me next link:

https://my.address.com/my-bucket-name/folder-name/file.pdf?AWSAccessKeyId=<key>&Signature=<signature>&x-amz-security-token=<toke>&Expires=<time>

There are two problems with this:

  • I don't want to expose my bucket name, so my-bucket-name/ should be ommited
  • This link doesn't work, I'm getting
<Code>SignatureDoesNotMatch</Code>
<Message>
The request signature we calculated does not match the signature you provided. Check your key and signing method.
</Message>

Those these are the questions:

  1. Is it possible to achieve a workable link without exposing bucket name?
  2. I've already read something about that custom domains are only possible for HTTP, not HTTPS access, is it true? What should I do in this case?
  3. The DNS alias wasn't made by me, so I'm not sure if it works or is set up correctly, what should I check/ask to verify that it will be working for s3?

Currently I'm a bit lost in Amazon docs. Also I'm new to all this AWS stuff.

halfer
  • 19,824
  • 17
  • 99
  • 186
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Feb 12 '20 at 07:59
  • @halfer Thanks for pointing it out. I felt something like this when was writing it. Will definitely keep this in mind as for future. –  Feb 12 '20 at 09:43

2 Answers2

3

It is not possible to hide the bucket name in an Amazon S3 pre-signed URL. This is because the request is being made to the bucket. The signature simply authorizes the request.

One way you could do it is to use Amazon CloudFront, with the bucket as the Origin. You can associate a domain name with the CloudFront distribution, which is unrelated to the Origin where CloudFront obtains its content.

Amazon CloudFront supports pre-signed URLs. You could give CloudFront access to the S3 bucket via an Origin Access Identity (OAI), then configure the distribution to be private. Then, access content via CloudFront pre-signed URLs. Please note that the whole content of the distribution would be private, so you would either need two CloudFront distributions (one public, one private), or only use CloudFront for the private portion (and continue using direct-to-S3 for the public portion).

If the whole website is private, then you could use a cookie with CloudFront instead of having to generate pre-signed URLs for every URL.

John Rotenstein
  • 241,921
  • 22
  • 380
  • 470
0

As far as I know, you cannot have a pre-signed URL without exposing the bucket name. Yes, you cannot access a custom domain name mapped to the S3 bucket URL via https. Because when you access https://example.com and example.com is mapped to my-bucket-name.s3.amazonaws.com, it is not possible for S3 to decrypt the SSL traffic. See this AWS docs page, Limitation section.

Arun Kamalanathan
  • 8,107
  • 4
  • 23
  • 39
  • Thanks for pointing both this things out. Found a page on aws docs for SSL limitations, requested an edit. –  Feb 12 '20 at 10:08