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

不想你离开。 提交于 2020-08-05 06:43:49

问题


Since Digital Ocean Spaces API is compatible with AWS SDK, how to upload images to Digital Ocean Spaces programmatically using AWS SDK for Yii2?

Here my details

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

I have tried with this code whis is not working

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

回答1:


You can php code to upload image in digital ocean:

  1. Configure a client:

    use 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. Create a New Space

    $client->createBucket([
        'Bucket' => 'example-space-name',
    ]);
    
  3. Upload Image

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



回答2:


Here how i mange to go this

Added the three libraries 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'
});

Now added the function // dev server

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());
});

Run the command

gulp dev



来源:https://stackoverflow.com/questions/60937975/how-to-upload-image-to-digital-ocean-spaces-using-aws-sdk-for-yii2

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!