Download the PHP package 1tomany/storage-bundle without Composer
On this page you can find all versions of the php package 1tomany/storage-bundle. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Informations about the package storage-bundle
Storage Bundle for Symfony
This bundle makes it easy to upload files to remote storage services like Amazon S3, Cloudflare R2, Google Cloud Storage, and Azure Blob Storage. Additionally, it provides a mock storage service to easily test your integrations without requiring a network connection.
Installation
Install the bundle using Composer:
If you're using Amazon S3 or an S3 compatible provider like Cloudflare R2, you'll also need to install the AWS SDK bundle provided by Amazon:
Going forward, any mention of Amazon S3 or AWS assumes you're using Amazon S3 itself or a compatible provider.
Updating .env
and .env.test
This bundle does not have a Symfony Flex recipe yet, so you'll have to manually update your .env
file by adding the following section:
And update the .env.test
file by adding the following section:
STORAGE_SERVICE
The storage provider to use. Possible values are:
aws
Amazon S3mock
A mock service for testing
STORAGE_BUCKET
The bucket where files will be uploaded.
STORAGE_CUSTOM_URL
The URL to use to reference the uploaded file instead of the canonical URL returned by the provider. Set this value if you use Amazon CloudFront or a public Cloudflare R2 bucket domain to get a publicly accessible file URL:
When set, if an object named users/10/files/avatar.png
was uploaded, the following URL would be returned:
Configuring Amazon S3
Installing the aws/aws-sdk-php-symfony
package will create a file named config/packages/aws.yaml
and update the .env
file with following section:
You should add the following environment variable for modern versions of Symfony as well:
I highly recommend taking advantage of Symfony secrets to store encrypted values of the AWS_KEY
and AWS_SECRET
environment variables and removing them directly from the .env
file.
Configuring Cloudflare R2
The Cloudflare R2 service is an Amazon S3 compatible provider, which means you can use the AWS SDK and bundle as is with one additional environment variable:
Replace <account_id>
with the account ID found in the Cloudflare R2 dashboard; it's usually a 32 character hexadecimal string like 45242ae44b7b9f01930a43d617f9f7a8
.
You'll also have to update the config/packages/aws.yaml
file to use a different region and this environment variable. Change the region
key from us-east-1
to auto
, and add the endpoint
key:
Using actions
This bundle registers a factory in the the Symfony container that will create a storage provider service object. Each storage provider service class implements a common interface: OneToMany\StorageBundle\Service\StorageServiceInterface
. When a variable of this type is injected, the Symfony container will create the concrete storage provider service object defined by the STORAGE_SERVICE
environment variable.
However, I do not recommend using the StorageServiceInterface
directly. Instead, you should use an action class. There are two action classes:
OneToMany\StorageBundle\Action\DownloadFileAction
OneToMany\StorageBundle\Action\UploadFileAction
Each of these expose a single public function, act()
, which calls the actual StorageServiceInterface
method to perform the action requested.
The code above would be rewritten as follows:
Action philosophy
The difference is subtle, but I prefer using the action classes for a few reasons:
- The class name indicates the action being performed:
UploadFileAction
clearly indicates that we're uploading a file. - Any non-provider-specific pre or post-processing computation can be handled in the
act()
method rather than reimplementing it in each storage provider class. - They can be mocked in tests easier. Because a concrete object is being injected, only the
act()
method needs to be mocked. Mocking (or creating an anonymous class of) an interface is more difficult and often overkill for a test that's only testing one codepath.
Credits
License
The MIT License
All versions of storage-bundle with dependencies
ext-fileinfo Version *
psr/container Version ^2.0
psr/http-message Version ^2.0
symfony/dependency-injection Version ^7.2
symfony/filesystem Version ^7.2
symfony/http-kernel Version ^7.2