Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/258.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
使用PHP DI在PHP中使用S3上载_Php_Amazon Web Services_Amazon S3 - Fatal编程技术网

使用PHP DI在PHP中使用S3上载

使用PHP DI在PHP中使用S3上载,php,amazon-web-services,amazon-s3,Php,Amazon Web Services,Amazon S3,我正在使用依赖项注入器设置S3的凭据: // AWS S3 for PDF $container['s3_pdf'] = function ($c) { // Only load credentials from environment variables. $provider = CredentialProvider::env(); $s3 = new Aws\S3\S3Client([ 'version' => 'latest',

我正在使用依赖项注入器设置S3的凭据:

// AWS S3 for PDF
$container['s3_pdf'] = function ($c) {
    // Only load credentials from environment variables.
    $provider = CredentialProvider::env();

    $s3 = new Aws\S3\S3Client([
        'version'     => 'latest',
        'region'      => 'ap-southeast-2',
        'credentials' => $provider
    ]);

    return $s3;
};
然后,每当我想上传一些东西时,我会:

$result = $this->s3_pdf->putObject(array(
    'Bucket'       => 'reports.omitted.com',
    'Key'          => 'temptest1.pdf',
    'SourceFile'   => 'assets/temp.pdf',
    'ContentType'  => 'text/plain',
    'ACL'          => 'public-read',
    'StorageClass' => 'REDUCED_REDUNDANCY',
    'Metadata'     => array(
        'param1' => 'value 1',
        'param2' => 'value 2'
    )
));
我希望能够从代码中的不同函数上传到S3,而不必每次都写入bucket名称,我是否能够让
S3\u pdf
容器返回一个函数,该函数只接受
sourcefile
并运行一些代码来确定源文件和目标并上传到S3


我知道我可以使用一个包含我所追求的函数的类,并在我需要S3的函数中使用该类的对象,但如果有办法,我宁愿使用依赖项容器。

这是我建议的包装函数的最简单示例:

class WhateverYourClassIs
{
  function putObject( $key, $sourceFile )
  {
    return $this->s3_pdf->putObject(array(
      'Bucket'       => 'reports.omitted.com',
      'Key'          => $ke,
      'SourceFile'   => $sourceFile,
      'ContentType'  => 'text/plain',
      'ACL'          => 'public-read',
      'StorageClass' => 'REDUCED_REDUNDANCY',
      'Metadata'     => array(
          'param1' => 'value 1',
          'param2' => 'value 2'
      )
    ));
  }
}
或者使用数组

class WhateverYourClassIs
{
  function putObject( $overloadedConfig )
  {
    $baseConfig = array(
      'Bucket'       => 'reports.omitted.com',
      'Key'          => NULL,
      'SourceFile'   => NULL,
      'ContentType'  => 'text/plain',
      'ACL'          => 'public-read',
      'StorageClass' => 'REDUCED_REDUNDANCY',
      'Metadata'     => array()
    );
    return $this->s3_pdf->putObject( array_merge_recursive( $baseConfig, $overloadedConfig ) );
  }
}

$this->putObject(array(
  'Key' => 'temptest1.pdf',
  'SourceFile' => assets/temp.pdf'
));

创建一个新的
$this->putObject()
函数,该函数包装
$this->s3\u pdf->putObject()
,并注入configuration@Scuzzy谢谢你的评论。如何将参数传递给该函数?我建议您可以将两个数组合并在一起,您的包装器函数提供基础,您可以输入覆盖参数。请参阅array\u merge()和偶数array\u merge\u recursive(),为什么投票失败?