可以将文章内容翻译成中文,广告屏蔽插件可能会导致该功能失效(如失效,请关闭广告屏蔽插件后再试):
问题:
Want to improve this question? Update the question so it's on-topic for Stack Overflow.
Closed 7 years ago.
How can I change the Amazon S3 url from http://bucket.amazons3.com/imagepath.jpg to http://image.mydomain.com/imagepath.jpg
EDIT:
Basically the whole reason for this is to hide the amazon s3 url from my users.
I was thinking about a HttpModule that would redirect the request from image.mydomain.com to bucket.amazons3.com. But that would require all requests to be handled by my servers first then forwarded.
回答1:
If I understand, you need to specify your domain name instead of S3. To do this, you need to write a handler which will act as a proxy to the amazon server.
But, that would actually double your bandwidth as you need to send the image to the client as well fetch the image for S3.
I am not sure is there any S3 specific way to handle this.
Below is Amazon S3 specific way taken from Amazon S3, CNAME record
The average user may sign up for
Amazon S3 hosting and begin uploading
files - accepting the default URL
structure for hosted files:
http://s3.amazonaws.com/your-bucket/some-file.txt
If you plan on using S3 to host media
files - more specifically Flash files
- you’ll run into Adobe’s cross domain security policy. The fix requires
mapping your hosted S3 files to look
as though they are being served from
your own domain - virtual hosting. The
easiest and most attractive method
would be a hosted file URL that like
this:
http://s3.your-site.com/some-file.txt
To get started, create a bucket on S3
that you want as the root for your
hosted files. For this example, your
S3 bucket would be:
s3.your-site.com The most important
step is adding the appropriate CNAME
record to your DNS settings.
Name Type Data
s3.your-site.com CNAME s3.amazonaws.com.
Expect your new DNS settings to take
up to 24-48 hours to resolve.
回答2:
Creating a CNAME on s3.amazonaws.com. is sufficient. There is no need to prepend the bucket name.
回答3:
To elaborate on Tez's answer, Amazon S3 provides support for mapping custom domains via Virtual Hosting of Buckets.
To make this work, the name of your bucket must be the full hostname you wish to map.
So, using the example in the original question, create an S3 bucket called image.mydomain.com
and set up a CNAME for it:
image.mydomain.com CNAME image.mydomain.com.s3.amazonaws.com.
回答4:
It sounds like you want to generate expiring links,
http://www.ioncannon.net/s3/21/creating-s3-urls-that-expire-using-php/
You can then generate these on the fly, and supply them as redirects via your application.
So someone requests,
http://images.mydomain.com/img123.jpg
and you generate a nice, auto-expiring s3 url,
http://mydomain.s3.amazonaws.com/T154456.jpg?AWSAccessKeyId=1ESOMESPECIALIDJJAKJ6RA82&Expires=1241372284&Signature=ddfr%2BlkoSEPAL%2BGbMwlMzj6q%2BCY%3D
then you redirect the user to the auto-generated url. this way, even if the user looks at the html source they only see the
url leading back to your site, but when they click the link, they are redirected to amazon so you don't have to worry about proxy-ing
the request via your server and eating up un-necessary bandwidth.
回答5:
Bryan's answer is correct and Ramesh and Vincent's have an error. Setting the CNAME to s3.amazonaws.com resulted in unnecessary redirects for me, as warned in the AWS Virtual Hosting of Buckets guide here.
回答6:
if you have control over your DNS why not setup a CNAME?
we have a cname setup pointing js.our-domain.com pointing to one of our buckets (oddly enough javascript files) we then just reference js.our-domain.com/jquery.js (for example) and away we go.
Depending upon your host, there are any number of ways to setup CNAME's - a google search reveals many
回答7:
The way i see it, you have two choices:
- to make the redirects as you stated, or
- to make a requests for the resources from S3 and then to return them to the users via your response.
So, if user wants to see the http://image.mydomain.com/imagepath.jpg resource, you can internally request the original resource from http://bucket.amazons3.com/imagepath.jpg, save it locally and return it to the requesting user.
It does spend a lot of bandwidth, but if this is your critical requirement, this should be the way to go.