Download the PHP package danilowa/laravel-easy-cloud-storage without Composer
On this page you can find all versions of the php package danilowa/laravel-easy-cloud-storage. It is possible to download/install these versions without Composer. Possible dependencies are resolved automatically.
Download danilowa/laravel-easy-cloud-storage
More information about danilowa/laravel-easy-cloud-storage
Files in danilowa/laravel-easy-cloud-storage
Package laravel-easy-cloud-storage
Short Description A robust and flexible Laravel package for managing files in various cloud storage providers.
License MIT
Homepage https://github.com/DaniloWA/laravel-easy-cloud-storage
Informations about the package laravel-easy-cloud-storage
Laravel EasyCloudStorage
Laravel EasyCloudStorage is a flexible and intuitive package designed to simplify cloud storage management within Laravel applications. It provides a clean interface for interacting with various storage providers, including local disks, Amazon S3, and Google Cloud Storage.
Table of Contents
- Installation
- Configuration
- Usage
- Initial Example
- File Operations
- Uploading Files
- Downloading Files
- Getting File URL
- Deleting Files
- Checking File Existence
- Copying Files
- Moving Files
- Getting File Size
- Getting Last Modified Time
- Getting Metadata
- Setting Metadata
- Listing Files
- Prepending and Appending Data
- Creating and Deleting Directories
- Error Handling
- Driver-Specific Method Availability
- Design Patterns: Facade and Contracts
- License
Installation
To install the package, run the following command via Composer:
After installation, the service provider will be automatically registered. To customize the configuration, publish the package's config file:
Configuration
The configuration file is located at config/easycloudstorage.php
. The main settings include:
Usage
After configuration, you can use the EasyCloudStorage package for various operations.
Initial Example
This section demonstrates how to perform a file upload using the EasyCloudStorage package. The example illustrates the process of uploading a file to a specified directory, along with optional logging and error handling configurations.
Explanation of the Code:
-
Use Statement:
- The
use
statement imports theEasyStorage
facade, which allows you to access storage functionalities easily.
- The
-
Uploading a File:
- The
upload
method is called on theEasyStorage
facade, taking in:$uploadedFile
: An instance ofUploadedFile
, typically obtained from a file upload in an HTTP request.'uploads/myfile.txt'
: The destination path where the file will be stored on the disk.
- The
-
Error Logging Configuration:
- The
withLog
method allows you to control error logging during the upload process:- Enabled (true): Error logging is activated for the upload operation.
- Disabled (false): Turns off error logging.
- Default Behavior: If called without parameters, it assumes
true
, enabling error logging by default. - Configuration Fallback: If not used, the package utilizes the error logging setting specified in the configuration file.
- The
-
Error Handling Configuration:
- The
withError
method configures whether to throw exceptions when errors occur during the upload process:- Throw Exceptions (true): System will throw exceptions for any encountered errors.
- Do Not Throw Exceptions (false): Prevents exceptions from being thrown.
- Default Behavior: If called without parameters, it defaults to
true
. - Configuration Fallback: Follows the error handling configuration specified in the configuration file.
- The
-
Setting the Disk:
- The
setDisk
method allows you to manually specify the storage disk to be used, overriding the default disk configuration.
- The
- Success Check:
- After attempting the upload, the result is stored in
$filePath
. This variable will either contain a string representing the path to the uploaded file orfalse
if the upload failed. - The
if
statement checks the value of$filePath
to determine the success of the upload:- Successful Upload: If
$filePath
is of typestring
, it prints a message displaying the path of the uploaded file. - Unsuccessful Upload: If
$filePath
isfalse
, it indicates that the upload failed, and an error message is displayed.
- Successful Upload: If
- After attempting the upload, the result is stored in
File Operations
Uploading Files
Upload a file to the specified path on the storage disk.
-
Parameters:
UploadedFile $file
: The file to upload.string $path
: The destination path for the upload.string|null $newName
: Optional. The new name for the file; the original name is used if not provided.
- Returns:
string|false
- The file path if successful;false
otherwise.
Downloading Files
Download a file from the specified path on the storage disk.
-
Parameters:
string $path
: The path of the file to download.string|null $newName
: Optional. The new name for the downloaded file; the original name is used if not provided.
-
Returns:
BinaryFileResponse
- Throws:
NotFoundHttpException
if the file does not exist.
Getting File URL
Retrieve the URL of the stored file.
-
Parameters:
string $path
: The path of the file.
- Returns:
string
- The URL of the file.
Deleting Files
Delete a file at the specified path.
-
Parameters:
string $path
: The path of the file to be deleted.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Checking File Existence
Check if a file exists at the specified path.
-
Parameters:
string $path
: The path of the file.
- Returns:
bool
- Returnstrue
if it exists,false
otherwise.
Copying Files
Copy a file from a source path to a destination path.
-
Parameters:
string $from
: The path of the source file.string $to
: The destination path.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Moving Files
Move or rename a file.
- Parameters:
string $from
: The old path of the file.- `string
$to`: The new path.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Getting File Size
Retrieve the size of a file in bytes.
-
Parameters:
string $path
: The path of the file.
- Returns:
int|null
- Returns the file size in bytes ornull
if the file does not exist.
Getting Last Modified Time
Get the last modified timestamp of a file.
-
Parameters:
string $path
: The path of the file.
- Returns:
int|null
- Returns the timestamp ornull
if the file does not exist.
Getting Metadata
Retrieve metadata for a file.
-
Parameters:
string $path
: The path of the file.
- Returns:
array|null
- Returns metadata ornull
if the file does not exist.
Setting Metadata
Set metadata for a file.
-
Parameters:
string $path
: The path of the file.array $metadata
: The metadata to set.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Listing Files
List files in a specified directory.
-
Parameters:
string $directory
: The directory path.
- Returns:
array
- An array of file names.
Prepending and Appending Data
Prepend or append data to a file.
-
Parameters:
string $path
: The path of the file.string $data
: The data to prepend or append.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Creating and Deleting Directories
Create or delete a directory.
-
Parameters:
string $directory
: The directory path.
- Returns:
bool
- Returnstrue
on success,false
otherwise.
Error Handling
You can enable error logging and exception throwing through the configuration or method chaining. Use the withLog()
and withError()
methods to customize error handling per operation.
Driver-Specific Method Availability
It's important to note that not all storage drivers will support every method available in the EasyCloudStorage package. The functionality provided depends exclusively on the capabilities of each specific driver, such as Google Cloud Storage, Amazon S3, and others.
For example, while uploading and deleting files are common operations across most drivers, certain methods like managing metadata or creating directories may not be supported by all drivers.
Rest assured, the EasyCloudStorage package has built-in logic to handle these discrepancies gracefully. If a method is not available for a specific driver, an informative error will be returned, ensuring that you are promptly notified of the limitation. This design allows you to implement your logic without the worry of unexpected failures, promoting a smooth development experience.
Design Patterns: Facade and Contracts
Facade
The Facade pattern in Laravel offers a simple and intuitive interface to access classes within the application’s service container. By utilizing the EasyStorage facade, you can invoke methods directly without having to resolve the underlying service each time. This approach not only streamlines your code but also improves readability, allowing you to concentrate on building features rather than managing complex dependencies. The result is a cleaner and more efficient API that enhances your development experience.
Contracts
Contracts in Laravel define the expected behaviors of services, providing a clear guideline for how they should operate. By utilizing contracts, you ensure that your application can easily switch between different implementations of a service without affecting the code that interacts with it. This flexibility is beneficial for developers, as it allows for easier updates or changes to the underlying logic without requiring significant code alterations.
For users, this means a more reliable and maintainable application. You can confidently integrate new features or optimize existing ones while ensuring that the core functionality remains intact. By adhering to this practice, you also promote a clean architecture that is easier to understand and extend, ultimately enhancing the long-term sustainability of your codebase.
License
This package is licensed under the MIT License. See the LICENSE file for details.