Cloudflare-Einrichtungsanleitung für MEGA S4

When using MEGA S4 object storage for content delivery, Cloudflare can act as an edge layer to improve delivery speed and add security controls. By putting Cloudflare in front of your S4 bucket, you can serve and cache your objects under your own custom domain, using Cloudflare’s global network for lower latency, faster load times, and reduced bandwidth costs.

There are two ways to set this up. Pick one, you do not need both.

  • Method 1: Cloudflare Worker. Serves your bucket through a Worker on a custom domain. Works on any Cloudflare plan.
  • Method 2: Object URLs with a CNAME. Points a subdomain at your bucket using a CNAME and rewrite rules. Some steps require a Cloudflare Enterprise account.

Step 1: Get your bucket URL

  1. Open your S4 section and click the bucket you want to use.
  2. Next to the bucket settings you will find the bucket URL.
  3. Copy the URL. It has the format BucketName.s3.region.megas4.com. By default the region is g (global), but you can change it.

To check the available regions:

  1. Click your profile image.
  2. Go to Settings.
  3. Go to Object storage.
  4. Choose your preferred endpoint. You can see the full endpoints list here.

Step 2: Set up Cloudflare for your domain

  1. Sign in to your Cloudflare account.
  2. Click Get Started (if adding a new site) or Add a Site (if you already have one).
  3. Enter your domain (for example cdn.example.com) and click Continue.
  4. Select a Cloudflare plan and click Continue.
  5. If your domain is not yet active on Cloudflare, follow the steps to update your nameservers to Cloudflare’s.

Step 3: Set up the Cloudflare Worker

  1. Install git on your local system.
  2. Install and configure Wrangler, the Workers command-line interface (CLI).
  3. Start a project in the Workers CLI using the template repository.
  4. Run the following commands, changing the Worker name (shown here as s4-cloudflare) to your project name:
  5. git clone https://github.com/meganz/s4-cloudflare s4-cloudflare
  6. cd s4-cloudflare
  7. npm install
  8. npx wrangler init 
  9. cloudflarenpm install
  10. Open wrangler.toml in a text editor.
  11. Fill the vars section with your setup info:
    • S4_ACCESS_KEY_ID = „ACCESS_KEY_ID“
    • S4_SECRET_ACCESS_KEY = „SECRET_ACCESS_KEY“
    • S4_DEFAULT_REGION = „REGION“
    • AWS_S4_BUCKET = „YOUR-BUCKET“
    • ALLOW_LIST_BUCKET = „true“ if you want to allow clients to list objects, otherwise false
  12. In name, enter the name you want to use for your Worker.
  13. In the routes pattern, enter your domain:{ pattern = „cdn.example.com“, custom_domain = true }
  14. Save the changes, then upload the Worker to your Cloudflare account:
  15. npx wrangler deploy

Your subdomain now points to your S4 bucket. You can access objects using your subdomain followed by the object name, for example cdn.example.com/ObjectName.

Tipp

Wrangler bietet weitere Konfigurationsoptionen, die Sie auf der Wrangler-Konfigurationsseite entdecken können.

Hinweis

Some steps in this method require a Cloudflare Enterprise account (the Host Header rewrite)

Step 1: Enable object URL access in MEGA S4

  1. Right-click the bucket containing the objects you want to serve via Cloudflare.
  2. Click Settings.
  3. Click Object URL access.
  4. Select Grant object URL access to the bucket.
  5. Click Update.
  6. Open the bucket and right-click an object you want to serve.
  7. Click Manage object URL.
  8. Copy the object URL and save it for later.

Step 2: Set up Cloudflare for your domain

  1. Follow the steps above for setting up your domain in the Cloudflare dashboard.
  2. Go to DNS > Records.
  3. Click + Add Record and configure as follows:
    • Type: CNAME
    • Name: the subdomain you want to use (for example cdn.example.com)
    • Target: paste the MEGA S4 bucket URL (for example s3.g.megas4.com/ID/BucketName)
    • Proxy Status: Enabled (Proxied) (orange cloud)
  4. Click Save.

Step 3: Create a URL Rewrite rule

  1. Go to Rules.
  2. Click + Create rule.
  3. Click URL Rewrite Rule.
  4. Enter a rule name.
  5. Select Custom filter expression.
  6. Set the Field to Hostname.
  7. Set the Operator to equals.
  8. Set the Value to the domain you want to use (for example cdn.example.com).
  9. Under Set rewrite parameters, click Path and click Rewrite to….
  10. Select Dynamic.
  11. Take the bucket name and the ID from the object URL you copied earlier (s3.g.megas4.com/ID/BucketName/ObjectName).
  12. Set the value to concat("ID/BucketName", http.request.uri.path).
  13. Set Query to Preserve.
  14. Click Deploy.

Step 4: Create an Origin rule

  1. Click + Create rule.
  2. Click Origin Rule.
  3. Select Custom filter expression.
  4. Set the Field to Hostname.
  5. Set the Operator to equals.
  6. Set the Value to the domain you want to use (for example cdn.example.com).
  7. Under Then…, select Host Header (this feature is only available for Cloudflare Enterprise accounts).
  8. Select Rewrite to… and enter s3.g.megas4.com.
  9. Set Destination Port to Preserve.
  10. Click Deploy.

Your subdomain now points to your S4 bucket. You can access objects using your subdomain followed by the object name, for example cdn.example.com/ObjectName