我实际上知道ZF2有一个名为aws-sdk-php-zf2
的aws-sdk-php模块,但我有一个使用简单sdk
的部分,我想在我的zf2控制器中使用它,而没有2个sdk;一个用于简单的PHP,另一个用于ZF2脚本。有什么方法可以让它工作吗?
以下是我在简单的 PHP 脚本中使用 aws-sdk 的工作方式:
require 'vendor/autoload.php';
use AwsS3S3Client;
use AwsS3ExceptionS3Exception;
// Instantiate an S3 client
$client = S3Client::factory(array(
'credentials' => array(
'key' => 'key',
'secret' => 'secret_key',
)
));
$bucket = 'bucket_name';
$keyname = 'project_name/file.ext';
$result = $client->deleteObject(array(
'Bucket' => $bucket,
'Key' => $keyname
));
print_r($result);
我怎样才能做到这一点?
通过作曲家安装后:
1)将其放入public/init_autoloader.php
文件中以设置整个应用程序中可用的库,这是我的:
// Composer autoloading
if (file_exists('vendor/autoload.php')) {
$loader = include 'vendor/autoload.php';
}
$zf2Path = false;
if (is_dir('vendor/ZF2/library')) {
$zf2Path = 'vendor/ZF2/library';
} elseif (getenv('ZF2_PATH')) { //Support for ZF2_PATH environment variable or git submodule
$zf2Path = getenv('ZF2_PATH');
} elseif (get_cfg_var('zf2_path')) { //Support for zf2_path directive value
$zf2Path = get_cfg_var('zf2_path');
}
if ($zf2Path) {
if (isset($loader)) {
$loader->add('Zend', $zf2Path);
} else {
include $zf2Path . '/Zend/Loader/AutoloaderFactory.php';
ZendLoaderAutoloaderFactory::factory(array(
'ZendLoaderStandardAutoloader' => array(
'autoregister_zf' => true
)
));
}
}
2)根据需要在控制器中使用它,在我的例子中,以下是控制器内的私有功能:
use AwsS3S3Client;
use AwsS3ExceptionS3Exception as S3Exception;
...
private function s3UploadFile($id, $invalidation=false, $file = null, $content = null){
$response = '';
//check if the file already exists in S3, if not then build it
try {
$s3Client = S3Client::factory(array(
'key' => $this->config['aws']['key'],
'secret' => $this->config['aws']['secret'],
'region' => $this->config['aws']['region']
));
if (!$s3Client->doesObjectExist('clients','/' . $id . '/' . $file))
$s3Client->putObject(array(
'Bucket' => 'clients',
'Key' => '/' . $clientId . '/' . $file,
'Body' => $content,
'ACL' => 'public-read'
));
} catch (S3Exception $e) {
$response = 'error';
}
return $response;
}
...
我希望这对你有所帮助。