php - 如何使用适用于 Yii2 的 AWS SDK 将图像上传到数字 Ocean Spaces?

标签 php yii2 digital-ocean

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代码在 digital ocean 中上传图片:

  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'
    ]);
    

关于php - 如何使用适用于 Yii2 的 AWS SDK 将图像上传到数字 Ocean Spaces?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60937975/

相关文章:

php - xsendFile 下载有 0 个字节

php - 从多对多表中检索分组

javascript - 如何在javascript中获取Kartik radioList的检查值

php - yii2 move_upload函数上传文件出错

node.js - node-cron 无法在我的服务器上运行

php - Facebook 忽略 og :image for shared website link

php - 如果只有同一用户会看到 XSS 输入是否构成威胁?

google-app-engine - 尝试从特定 IP 访问 appengine.google.com 时出现 403 禁止错误

internationalization - 在 Yii2 语言字符串中包含一个 html 元素

python - 无法访问 postgresql 表