Report this

What is the reason for this report?

How to prevent hotlinking of files hosted in Digital Ocean Spaces?

Posted on December 14, 2017

I just recently move from S3 to Spaces. I’d like to know if Space supports Bucket Policy like S3 and how to prevent files hosted in Spaces from being hotlinked? I only need a very basic one like filtering the referrer header before serving the file



This textbox defaults to using Markdown to format your answer.

You can type !ref in this text area to quickly search our full set of tutorials, documentation & marketplace offerings and insert the link!

These answers are provided by our Community. If you find them useful, show some love by clicking the heart. If you run into issues leave a comment, or add your own answer to help others.

Support for bucket policies is in the works. In fact, for many basic use cases it is available via the API today. Though at this time, support is not available in the UI nor is it officially documented. There may be inconsistencies between regions and certain features may not work as expected just yet. Consider it an alpha release for all intents and purposes. If that hasn’t scared you off just yet, here is an example that I’ve tested and confirmed to work in NYC3.

Using the AWS cli, you can post a bucket policy with:

aws s3api --endpoint-url=https://nyc3.digitaloceanspaces.com \
    put-bucket-policy \
    --bucket examplebucket \
    --policy file://policy.json

Where the contents of policy.json look like:

{
    "Statement": [
        {
            "Effect": "Allow",
            "Principal": "*",
            "Action": "s3:GetObject",
            "Resource": "arn:aws:s3:::examplebucket/*",
            "Condition": {
                "StringLike": {
                    "aws:Referer": [
                        "http://example.com/*"
                    ]
                }
            }
        },
        {
          "Effect": "Deny",
          "Principal": {
            "AWS": "*"
          },
          "Action": "s3:GetObject",
          "Resource": "arn:aws:s3:::examplebucket/*",
          "Condition": {
            "StringNotLike": {
              "aws:Referer": [
                "http://example.com/*"
              ]
            }
          }
        }
    ]
}

This specifies that objects in the bucket are accessible when the referer is example.com. Any other referer will return a 403 (Forbidden) instead.

It’s important to note that this does not prevent someone from accessing the object directly or downloading it, just embedding it into their site.

I need to know any updates on this ? As putting CDN ahead is another cost addon for me and wish to restrict & disable hotlinking of all files put up on Digital Ocean Spaces.

Does DigitalOcean offer any official way to prevent hotlinking yet?

The developer cloud

Scale up as you grow — whether you're running one virtual machine or ten thousand.

Get started for free

Sign up and get $200 in credit for your first 60 days with DigitalOcean.*

*This promotional offer applies to new accounts only.