|
| 1 | +--- |
| 2 | +myst: |
| 3 | + html_meta: |
| 4 | + description: Configure remote storage for Magento 2.x. Learn how to configure |
| 5 | + Magento 2 to start storing files in your bucket using a single command. |
| 6 | + title: How to Configure Remote Storage for Magento 2.x | Hypernode |
| 7 | +--- |
| 8 | + |
| 9 | +# How to Configure Remote Storage for Magento 2.x |
| 10 | + |
| 11 | +Magento 2.x supports remote storage for media files, import/export files, and other files. |
| 12 | +This feature allows you to store files in a remote storage location, such as an Amazon S3 bucket, instead of storing them on the server itself. |
| 13 | + |
| 14 | +This can be useful for many reasons, such as: |
| 15 | + |
| 16 | +- Offloading storage from your server, reducing the load on your server, and improving performance. |
| 17 | +- Allows you to make use of [horizontal scaling](../../hypernode-platform/autoscaling/how-does-horizontal-autoscaling-work), as you can easily add more servers without having to worry about syncing files between them. |
| 18 | +- Allows for effortless storage capacity scaling, as you can easily increase the storage capacity of your remote storage location. |
| 19 | +- Serving assets from a CDN, which can improve the performance of your website. |
| 20 | + |
| 21 | +## Configuring the application |
| 22 | + |
| 23 | +Configuring Magento 2 to start storing files in your bucket is done using a single command. |
| 24 | + |
| 25 | +**AWS S3** |
| 26 | + |
| 27 | +```bash |
| 28 | +bin/magento setup:config:set \ |
| 29 | + --remote-storage-driver="aws-s3" \ |
| 30 | + --remote-storage-bucket="my_bucket_name" \ |
| 31 | + --remote-storage-region="my-aws-region" \ |
| 32 | + --remote-storage-key="abcd1234" \ |
| 33 | + --remote-storage-secret="abcd1234" |
| 34 | +``` |
| 35 | + |
| 36 | +**Other S3 compatible providers** |
| 37 | + |
| 38 | +If you're using a different provider than AWS S3, you need to specify the `--remote-storage-endpoint` option. |
| 39 | + |
| 40 | +```bash |
| 41 | +bin/magento setup:config:set \ |
| 42 | + --remote-storage-driver="aws-s3" \ |
| 43 | + --remote-storage-bucket="my_bucket_name" \ |
| 44 | + --remote-storage-region="provider-region" \ |
| 45 | + --remote-storage-key="abcd1234" \ |
| 46 | + --remote-storage-secret="abcd1234" \ |
| 47 | + --remote-storage-endpoint="https://my-s3-compatible.endpoint.com" |
| 48 | +``` |
| 49 | + |
| 50 | +## Syncing the files |
| 51 | + |
| 52 | +Instead of running (which is Magento's official way to do this): |
| 53 | + |
| 54 | +```bash |
| 55 | +bin/magento remote-storage:sync |
| 56 | +``` |
| 57 | + |
| 58 | +One can run the following instead to really speed up the process: |
| 59 | + |
| 60 | +```bash |
| 61 | +aws s3 sync pub/media/ s3://my_bucket_name/media/ |
| 62 | +aws s3 sync var/import_export s3://my_bucket_name/import_export |
| 63 | +``` |
| 64 | + |
| 65 | +This is much faster than Magento's built-in sync, because `aws s3 sync` uploads files concurrently. |
| 66 | + |
| 67 | +## The storage flag file in the bucket |
| 68 | + |
| 69 | +Magento's S3 implementation creates a test file called `storage.flag`, which is basically created to test if the connection works. So this is not a magic file to mark anything ([source](https://github.com/magento/magento2/blob/6f4805f82bb7511f72935daa493d48ebda3d9039/app/code/Magento/AwsS3/Driver/AwsS3.php#L104)). |
| 70 | + |
| 71 | +## Serving assets from your S3 bucket |
| 72 | + |
| 73 | +To start serving media assets from your S3 bucket, you need to make some adjustments to your nginx configuration. |
| 74 | + |
| 75 | +```nginx |
| 76 | +location /media { |
| 77 | + # ... |
| 78 | + location ~* \.(ico|jpg|jpeg|png|gif|svg|js|css|swf|eot|ttf|otf|woff|woff2)$ { |
| 79 | + resolver 8.8.8.8; |
| 80 | + set $bucket "<my_bucket_name>"; |
| 81 | + proxy_pass https://s3.amazonaws.com/$bucket$uri; |
| 82 | + proxy_pass_request_body off; |
| 83 | + proxy_pass_request_headers off; |
| 84 | + proxy_intercept_errors on; |
| 85 | + proxy_hide_header "x-amz-id-2"; |
| 86 | + proxy_hide_header "x-amz-request-id"; |
| 87 | + proxy_hide_header "x-amz-storage-class"; |
| 88 | + proxy_hide_header "Set-Cookie"; |
| 89 | + proxy_ignore_headers "Set-Cookie"; |
| 90 | + } |
| 91 | + # ... |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +Also make sure your S3 bucket policies are configured correctly, so that only `/media` is publicly readable. For example: |
| 96 | + |
| 97 | +```json |
| 98 | +{ |
| 99 | + "Version": "2012-10-17", |
| 100 | + "Statement": [ |
| 101 | + { |
| 102 | + "Sid": "AllowPublicReadOnlyForMedia", |
| 103 | + "Effect": "Allow", |
| 104 | + "Principal": "*", |
| 105 | + "Action": "s3:GetObject", |
| 106 | + "Resource": "arn:aws:s3:::<your-bucket-name>/media/*" |
| 107 | + } |
| 108 | + ] |
| 109 | +} |
| 110 | +``` |
| 111 | + |
| 112 | +## Magento remote storage documentation |
| 113 | + |
| 114 | +- [Configure Remote Storage](https://experienceleague.adobe.com/en/docs/commerce-operations/configuration-guide/storage/remote-storage/remote-storage) |
| 115 | +- [Configure AWS S3 bucket for remote storage](https://experienceleague.adobe.com/en/docs/commerce-operations/configuration-guide/storage/remote-storage/remote-storage-aws-s3) |
0 commit comments