How do I display protected Amazon S3 images on my secure site using PHP?

前端 未结 4 2028
太阳男子
太阳男子 2021-01-30 17:39

I am trying to move images for my site from my host to Amazon S3 cloud hosting. These images are of client work sites and cannot be publicly available. I would like them to be d

4条回答
  •  面向向阳花
    2021-01-30 18:11

    You can use bucket policies in your Amazon bucket to allow your application's domain to access the file. In fact, you can even add your local dev domain (ex: mylocaldomain.local) to the access list and you will be able to get your images. Amazon provides sample bucket policies here: http://docs.aws.amazon.com/AmazonS3/latest/dev/AccessPolicyLanguage_UseCases_s3_a.html. This was very helpful to help me serve my images.

    The policy below solved the problem that brought me to this SO topic:

        {
           "Version":"2008-10-17",
           "Id":"http referer policy example",
           "Statement":[
        {
          "Sid":"Allow get requests originated from www.example.com and example.com",
          "Effect":"Allow",
          "Principal":"*",
          "Action":"s3:GetObject",
          "Resource":"arn:aws:s3:::examplebucket/*",
          "Condition":{
            "StringLike":{
              "aws:Referer":[
                "http://www.example.com/*",
                "http://example.com/*"
              ]
            }
          }
        }
      ]
    }
    

提交回复
热议问题