PHPackages                             1tomany/storage-bundle - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. 1tomany/storage-bundle

ActiveSymfony-bundle[File &amp; Storage](/categories/file-storage)

1tomany/storage-bundle
======================

Symfony bundle to manage uploading and downloading files to and from remote storage services

v3.1.1(2mo ago)0294↓50%MITPHPPHP &gt;=8.4

Since Apr 2Pushed 2mo ago2 watchersCompare

[ Source](https://github.com/1tomany/storage-bundle)[ Packagist](https://packagist.org/packages/1tomany/storage-bundle)[ RSS](/packages/1tomany-storage-bundle/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (7)Dependencies (18)Versions (22)Used By (0)

Storage Bundle for Symfony
==========================

[](#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 client to easily test your integrations without requiring a network connection.

Installation
------------

[](#installation)

Install the bundle using Composer:

```
composer require 1tomany/storage-bundle

```

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:

```
composer require aws/aws-sdk-php-symfony

```

Going forward, any mention of Amazon S3 or AWS assumes you're using Amazon S3 itself or a compatible provider.

Configuration
-------------

[](#configuration)

Below is the complete configuration for this bundle. To customize it for your Symfony application, create a file named `onetomany_storage.yaml` in `config/packages/` and make the necessary changes.

```
onetomany_storage:
    client: "amazon"
    bucket: "storage-bucket"
    custom_url: ~

    amazon_client:
        bucket: "amazon-bucket"
        custom_url: "https://dev.app-cdn.com"
        s3_client: "s3_client_service_id"

    mock_client:
        bucket: "mock-bucket"
        custom_url: "https://mock.app-cdn.com"
```

#### `onetomany_storage.client`

[](#onetomany_storageclient)

The storage client to use. Possible values are:

- `"amazon"` Amazon S3 compatible client
- `"mock"` A mock client for testing

These values correspond to the `key` for each service with the tag `onetomany.storage.client`. You can add your own client by implementing the `OneToMany\StorageBundle\Contract\Client\ClientInterface` interface and tagging it with the tag `onetomany.storage.client` and a `key` value other than the ones above.

#### `onetomany_storage.client`

[](#onetomany_storageclient-1)

The bucket where files will be uploaded.

#### `onetomany_storage.custom_url`

[](#onetomany_storagecustom_url)

The URL used to reference the uploaded file instead of the canonical URL returned by the storage service. Set this value if you use Amazon CloudFront or a public Cloudflare R2 bucket domain to get a publicly accessible file URL:

```
onetomany_storage:
    custom_url: "https://files.app-cdn.com"
```

When set, if an object with the key `users/10/files/avatar.png` was uploaded, the following URL would be returned:

```
https://files.app-cdn.com/users/10/files/avatar.png

```

### Configuring Amazon S3

[](#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:

```
###> aws/aws-sdk-php-symfony ###
AWS_KEY=not-a-real-key
AWS_SECRET=@@not-a-real-secret
###< aws/aws-sdk-php-symfony ###
```

You should add the following environment variable for modern versions of Symfony as well:

```
AWS_MERGE_CONFIG=true
```

I highly recommend taking advantage of [Symfony secrets](https://symfony.com/doc/current/configuration/secrets.html) to store encrypted values of the `AWS_KEY` and `AWS_SECRET` environment variables and removing them directly from the `.env` file.

### Configuring Cloudflare R2

[](#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:

```
AWS_ENDPOINT="https://.r2.cloudflarestorage.com"
```

Replace `` 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:

```
aws:
    version: latest
    region: auto
    endpoint: "%env(AWS_ENDPOINT)%"
    credentials:
        key: "%env(AWS_KEY)%"
        secret: "%env(AWS_SECRET)%"
```

Using actions
-------------

[](#using-actions)

This bundle registers a factory in the the Symfony container that will create a storage client object. Each storage client class implements a common interface: `OneToMany\StorageBundle\Contract\Client\ClientInterface`. When an object of this type is injected into a class, the Symfony container will create the client object defined by the value stored in the `onetomany_storage.client` property.

```
