gpt4 book ai didi

php - 如何使用适用于 Yii2 的 AWS SDK 将图像上传到 digital ocean 空间?

转载 作者:行者123 更新时间:2023-12-02 02:56:47 25 4
gpt4 key购买 nike

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 将图像上传到 digital ocean 空间?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/60937975/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com