如何使用适用于 Yii2 的 AWS SDK 将图像上传到数字海洋空间?

How to upload image to digital Ocean Spaces using AWS SDK for Yii2?

Since Digital Ocean Spaces API is compatible with AWS SDK, how to upload images to Digital Ocean Spaces programmatically using AWS SDK for 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代码在数字海洋中上传图像:

  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. 新建一个Space

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

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

这是我如何完成的

添加了三个库 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 dev