wamland/wam-amazon-s3-php

此包的最新版本(dev-master)没有可用的许可证信息。

此服务允许在Amazon S3上发送和接收文件

dev-master 2017-06-27 00:29 UTC

This package is not auto-updated.

Last update: 2024-09-29 02:55:43 UTC


README

此服务允许在Amazon S3上发送和接收文件

恢复源代码

composer require wamland/wam-amazon-s3-php:dev-master

创建包含环境变量的文件

cp vendor/wamland/wam-amazon-s3-php/.env_sample.php .env.php
vim .env.sh

默认配置

// AWS S3
define('AWS_ACL', 'public-read');
define('AWS_VERSION', 'latest');
define('AWS_REGION', 'ap-southeast-2');

define('AWS_ACCESS_KEY_ID', '');
define('AWS_SECRET_ACCESS_KEY', '');
define('AWS_BUCKET', '');

加载库

require('.env.php');
require('vendor/autoload.php');

初始化Amazon S3连接器

use App\Services\S3Service;

$s3 = new S3Service(
    AWS_REGION,
    AWS_VERSION,
    AWS_ACCESS_KEY_ID,
    AWS_SECRET_ACCESS_KEY
);

$s3->bucket = AWS_BUCKET;

上传媒体文件

$s3->file = 'src/resources/assets/fixtures/pic.jpg';
$s3->key = 'pic.jpg'; // If the key is not set, the file name will be used

try {
    $s3->put();
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

检索媒体文件

$s3->key = 'src/resources/assets/fixtures/pic.jpg';

try {
    $media = $s3->get();
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

删除项目

try {
    $s3->delete();
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

单元测试(Kahlan

composer test

完整发送和接收示例

<?php
require_once('.env.php');
require_once('vendor/autoload.php');

use App\Services\S3Service;

$s3 = new S3Service(
    AWS_REGION,
    AWS_VERSION,
    AWS_ACCESS_KEY_ID,
    AWS_SECRET_ACCESS_KEY
);

$s3->bucket = AWS_BUCKET;
$s3->file = 'src/resources/assets/fixtures/pic.jpg';

/**
 * |-------------------------------------------
 * | Uploading a media to amazon S3
 * |-------------------------------------------
 */
try {
    $s3->put();
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

/**
 * |-------------------------------------------
 * | Retrieving a media from Amazon S3
 * |-------------------------------------------
 */
try {
    $media = $s3->get();
} catch (\Exception $e) {
    echo $e->getMessage();
    die();
}

/**
 * |-------------------------------------------
 * | Display of the image previously sent
 * |-------------------------------------------
 */
echo sprintf("<img src='%s'>", $media['@metadata']['effectiveUri']);