【问题标题】:How to upload image to digital Ocean Spaces using AWS SDK for Yii2?如何使用 AWS SDK for Yii2 将图像上传到数字海洋空间?
【发布时间】:2020-07-11 06:14:40
【问题描述】:

由于 Digital Ocean Spaces API 与 AWS SDK 兼容,如何 使用 AWS 开发工具包以编程方式将图像上传到 Digital Ocean Spaces Yii2?

这里是我的详细信息

Good, we have the following data: 
1. endpoint: fra1.digitaloceanspaces.com
2. bucket name: dev-abc
3. api key: xxxxxxxxxxxxx and api secret: xxxxxxx
4. The url that you need to use to deliver assets is https://dev-abc

我已经尝试过这段代码,但它不起作用

$uploader = new FileUpload(FileUpload::S_S3, [
    'version' => 'latest',
    'region' => 'fra1',
    'endpoint' => 'https://fra1.digitaloceanspaces.com',
    'credentials' => [
        'key' => 'xxxxxxxxxxxxx ',
        'secret' => 'xxxxxxx'
    ],
    'bucket' => 'dev-abc'
]);

【问题讨论】:

    标签: php yii2 digital-ocean


    【解决方案1】:

    您可以通过php代码上传数字海洋中的图片:

    1. 配置客户端:

      使用 Aws\S3\S3Client;

      $client = new Aws\S3\S3Client([
          'version' => 'latest',
          'region'  => 'us-east-1',
          'endpoint' => 'https://nyc3.digitaloceanspaces.com',
          'credentials' => [
              'key'    => getenv('SPACES_KEY'),
              'secret' => getenv('SPACES_SECRET'),
          ],
      ]);
      
    2. 创建一个新空间

      $client->createBucket([
          'Bucket' => 'example-space-name',
      ]);
      
    3. 上传图片

      $client->putObject([
          'Bucket' => 'example-space-name',
          'Key'    => 'file.ext',
          'Body'   => 'The contents of the file.',
          'ACL'    => 'private'
      ]);
      

    【讨论】:

      【解决方案2】:

      我是如何做到这一点的

      添加了三个库 gulp,gulp-awspublish,gulp-rename

      var gulp = require('gulp');
      var awspublish = require('gulp-awspublish');
      var rename = require('gulp-rename');
      
      var publisherDev = awspublish.create({
        region: 'fra1',
        params: {
          Bucket: 'dev-static-abc-ro'
        },
        accessKeyId: 'XCCCCCZX',
        secretAccessKey: 'EDKDJKJDKDJ',
        endpoint: 'fra1.digitaloceanspaces.com'
      });
      

      现在添加了功能 // 开发服务器

      gulp.task('dev', function() {
        // console.log("Hi! I'm Gulp default task root!");
        return gulp
          .src('./temp-dist/**')
          .pipe(
            rename(function(path) {
              path.dirname += '/assets';
              // path.basename += "-s3";
            })
          )
          .pipe(publisherDev.publish())
          .pipe(publisherDev.sync('assets/'))
          .pipe(awspublish.reporter());
      });
      

      运行命令

      gulp 开发

      【讨论】:

        猜你喜欢
        • 2021-12-26
        • 2018-06-10
        • 1970-01-01
        • 2016-03-02
        • 2021-04-25
        • 2019-03-12
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多