AngularJs Image upload to S3

前端 未结 2 1684
南方客
南方客 2021-01-13 17:03

I am:
- Creating a Web Application
- AngularJS front end with ng-file upload (https://github.com/danialfarid/ng-file-upload)
- Node.js backend
- Want to be a

相关标签:
2条回答
  • 2021-01-13 17:33

    The example contains something I would consider an error of oversimplification.

    var s3Url = 'https://' + aws.bucket + '.s3-' + aws.region + '.amazonaws.com';
    

    This works much of the time, but it is not a consistently valid way of crafting a URL to an object in S3.

    It breaks in at least two cases, one of which is the one you've encountered.

    The endpoint for the US Standard region of S3, which is located in the us-east-1 region of AWS, is not s3-us-east-1.amazonaws.com, as it would be if it used the same format for all other regions. For what appear to be legacy/evolutionary reasons, it is simply s3.amazonaws.com but can also be written s3-external-1.amazonaws.com. (Remember, S3 has been around for almost ten years as of this writing, and the service has expanded and evolved, while maintaining backwards compatibiliy -- a noteworthy feat, but bound to result in some conventions that seen confusing at first glance.)

    However, all buckets -- including those not in us-east-1 -- but excluding those that would break the above code for the second reason (which I haven't gotten to yet) -- can be addressed simply as bucket-name.s3.amazonaws.com -- if you think about the hierarchical nature of DNS, you can see how this might work: S3, within a few minutes after a bucket is created, remaps that specific hostname, in DNS, to send the request to the correct regional S3 endpoint.

    So the + '.s3-' + aws.region + should work if written, simply, + '.s3' +.

    ...unless, of course, you have created a bucket with dots in its name. In this case, you will have a problem with https (this is problem #2, alluded to above), because a bucket with dots in the name will not match the wildcard SSL (TLS) certificate presented by S3 (a design limitation in SSL wildcard certificates, not S3 itself).

    If this is an issue, and using a bucket with no dots in the name is undesirable for some reason, your URLs must be crafted in what's called path-style format. This is the alternative to virtual-style format, where the bucket name is in the hostname. Here, the bucket name is the first part of the path:

    https://s3[-region].amazonaws.com/bucket.name.with.dots/key-path
    

    ...and, again, the first component is just s3 or s3-external-1 for US Standard (us-east-1)... but using this format requires you to match the region, unlike the above, where DNS handles the request routing... otherwise S3 will throw a permanent redirect error.

    http://docs.aws.amazon.com/AmazonS3/latest/dev/VirtualHosting.html

    http://docs.aws.amazon.com/general/latest/gr/rande.html#s3_region

    This is a lot of info, but hopefully useful in explaining not only what you need to do differently, but also why.

    0 讨论(0)
  • 2021-01-13 17:33

    Your front-end machine is failing to resolve the hostname via the domain name service. The most likely problem is that you used the wrong hostname for the server you are uploading to:

    mybucket.name.s3-us-east-1.amazonaws.com

    If the hostname being used is correct, verify that the DNS information for the host you are trying to reach is available from your client. For example, use the dig command under linux or nslookup under Windows.

    0 讨论(0)
提交回复
热议问题