如何使用适用于Yii2的AWS开发工具包将图像上传到数字海洋空间?

问题描述 投票:0回答:1

由于Digital Ocean Spaces API与AWS开发工具包兼容,因此如何使用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个回答
0
投票

您可以通过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'
    ]);
    
© www.soinside.com 2019 - 2024. All rights reserved.