jaimevalasek/jv-s3-upload

一个通用的AWS Amazon S3上传模块,适用于ZF2。

dev-master 2013-09-27 17:43 UTC

This package is not auto-updated.

Last update: 2024-09-24 05:05:35 UTC


README

创建者:Jaime Marcelo Valasek

用于将文件上传到您的Amazon S3桶的模块。

此模块仅用于AWS S3上传文件的用途。

未来的视频教程可以在网站http://www.zf2.com.br/tutoriais或YouTube频道http://www.youtube.com/zf2tutoriais上开发和发布。

安装

将此模块下载到您的vendor文件夹中。

完成上述步骤后,打开config/application.config.php文件。并添加名为JVS3Upload的模块。

使用composer

  1. 在您的composer.json中添加此项目和JVS3Upload。
"require": {
    "jaimevalasek/jv-s3-upload": "dev-master"
}
  1. 现在运行以下命令让composer下载JVS3Upload:

php composer.phar update

在您的application.config.php中启用它。

<?php
return array(
    'modules' => array(
        // ...
        'JVS3Upload',
    ),
    // ...
);

配置凭证

  • 在global config/autoload/aws.local.php文件夹中创建一个文件,并添加以下代码:
<?php

return array(
     'Aws' => array(
         'key'    => 'KEY',
         'secret' => 'SECRET_KEY'
     )
);

上传文件

在此示例中,我们使用了图像上传,但可以轻松地上传任何类型的文件。

// pegando a imagem para fazer upload
$fileInfo = $file->getFileInfo();

// nome do arquivo exemplo usando o filtro jvbase_filter_token do JVBase
$nomeDoArquivo = $this->getServiceLocator()->get('jvbase_filter_token')->microtimeToken() . "_" . $fileInfo['thumb_produto']['name'];
		    
// Setando o destino do arquivo dentro do bucket
$nameDestination = "contents/upload/imagem/{$nomeDoArquivo}";

// setando o nome do bucket
$bucket = 'nomedobucket';
		    
// Pegando as configurações do Service Locator
$config = $this->getServiceLocator()->get('config');
		    
// Enviando a imagem para o s3
$s3 = new S3($config["Aws"]["key"], $config["Aws"]["secret"]);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
		    
if($s3->putObjectFile($fileInfo['imagem']['tmp_name'], $bucket , $nameDestination, S3::ACL_PUBLIC_READ) )
{
	// Se chegar aqui é porque o upload foi um sucesso
} 
else 
{
	// Se chegar aqui é porque ocorreu um erro ao fazer o upload
}

上传带缩略图的图像

// pegando a imagem para fazer upload
$fileInfo = $file->getFileInfo();

// nome do arquivo exemplo usando o filtro jvbase_filter_token do JVBase
$nomeDoArquivo = $this->getServiceLocator()->get('jvbase_filter_token')->microtimeToken() . "_" . $fileInfo['thumb_produto']['name'];
		    
// Setando o destino do arquivo dentro do bucket
$nameDestination = "contents/upload/imagem/{$nomeDoArquivo}";
$nameDestinationThumb = "contents/upload/imagem/thumb/{$nomeDoArquivo}";

// setando o nome do bucket
$bucket = 'nomedobucket';
		    
// Pegando as configurações do Service Locator
$config = $this->getServiceLocator()->get('config');
		    
/* 
 * gerando a miniatura e enviando para o S3
 */
$s3Thumb = new S3Thumb($config, $bucket);
$s3Thumb->putThumb($fileInfo['imagem'], $nameDestinationThumb, 'public-read');
		    
// Enviando a imagem para o s3
$s3 = new S3($config["Aws"]["key"], $config["Aws"]["secret"]);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
		    
if($s3->putObjectFile($fileInfo['imagem']['tmp_name'], $bucket , $nameDestination, S3::ACL_PUBLIC_READ) )
{
	// Se chegar aqui é porque o upload foi um sucesso
} 
else 
{
	// Se chegar aqui é porque ocorreu um erro ao fazer o upload
}

在AWS S3中删除文件

// pegando a imagem para fazer upload
$fileInfo = $file->getFileInfo();

// nome do arquivo exemplo usando o filtro jvbase_filter_token do JVBase
$nomeDoArquivo = $this->params('arquivo');
		    
// Setando o destino do arquivo dentro do bucket
$deleteDestination = "contents/upload/imagem/{$nomeDoArquivo}";

// setando o nome do bucket
$bucket = 'nomedobucket';
		    
// Pegando as configurações do Service Locator
$config = $this->getServiceLocator()->get('config');
		    
// Instanciando
$s3 = new S3($config["Aws"]["key"], $config["Aws"]["secret"]);
$s3->putBucket($bucket, S3::ACL_PUBLIC_READ);
		    
if($s3->deleteObject($bucket, $deleteDestination))
{
	// Se chegar aqui é porque o arquivo foi excluido
} 
else 
{
	// Se chegar aqui é porque o arquivo não foi excluido
}