php 亚马逊AWS-S3对象存储上传文件

最近做国外项目的时候,需要把文件上传到AWS-S3对象存储空间里,下面整理一下上传方法,和碰到的问题

代码

 /**
 * 亚马逊oss Aws上传
 * composer require aws/aws-sdk-php
 * @param $filePath
 * @param $ossPath
 * @return array
 * @author wzb
 * @data 2024/5/25
 */
function ossAwsUploadFile($filePath = '', $ossPath = '')
{
    // 配置信息  composer require aws/aws-sdk-php
    $configOss = config('aws_oss');
    $accessKeyId = $configOss['accessKeyId'] ?? '';  // 你的AccessKeyId
    $accessKeySecret = $configOss['accessKeySecret'] ?? '';  // 你的AccessKeySecret
    $endpoint = $configOss['region'] ?? ''; // 你的Bucket所在地域的域名 ap-southeast-1
    $bucket = $configOss['bucket'] ?? ''; // 你的Bucket名字
    if (empty($accessKeyId) || empty($accessKeySecret) || empty($endpoint) || empty($bucket)) {
        return [];
    }
    $awsConfig = [
        'version' => 'latest',//版本
        'acl' => 'public-read',//权限//这个一定要加,是访问权限
        'bucket' => $bucket,//存储桶名称
        'region' => $endpoint,
        'key_id' => $accessKeyId,//Access key ID
        'access_key' => $accessKeySecret,//Secret access key
    ];
    //实例化
    $s3 = new S3Client([
        'version' => $awsConfig['version'],//版本
        'region' => $awsConfig['region'],//区域
        'credentials' => new Credentials(
            $awsConfig['key_id'],
            $awsConfig['access_key']
        ),
        // 开启bug调试
//            'debug' => true
    ]);
    if (!file_exists($filePath)) {
        return [];
    }
    try {
        $result = $s3->putObject([
            'Bucket' => $awsConfig['bucket'],
            'ACL' => $awsConfig['acl'],//这个一定要加,是访问权限
            'Key' => $ossPath,   // //亚马逊静态资源服务器上的路径+图片名称
//                'Body' => fopen($filePath, 'r'),
            'SourceFile' => $filePath,  // 原文件路径
        ]);
        $result = $result->toArray();
        $imgUrl = $result['ObjectURL'] ?? '';
        return $result;
    } catch (Exception $exception) {
        echo $exception->getMessage();
        return [];
    }
}

// 调用示例
 $ossInfo = ossAwsUploadFile("F:/wzb/img/5836923c44342.jpg", 'img/5836923c44342.jpg');

碰到的问题

一直报AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate的问题:
下面详细报错信息:
PutObject" on “https://ss.amazonaws.com/img/5836923c44342.jpg”; AWS HTTP error: cURL error 60: SSL certificate problem: unable to get local issuer certificate (see https://curl.haxx.se/libcurl/c/libcurl-errors.html) for https://ss.amazonaws.com/img/5836923c44342.jpg

解决办法

php.ini里面的 curl.cainfo 需要设置

  1. 下载cacert: https://curl.haxx.se/ca/cacert.pem

  2. 配置php.ini 并重启

curl.cainfo = "真实路径/cacert.pem"
  • 6
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
以下是使用 aws-sdk-s3 和 Minio C++ 库在 Visual Studio 中上传和下载文件的示例代码: ```c++ #include <aws/core/Aws.h> #include <aws/s3/S3Client.h> #include <aws/s3/model/PutObjectRequest.h> #include <aws/s3/model/GetObjectRequest.h> #include <iostream> #include <fstream> #include <minio/minio.h> #include <minio/minio_io.h> using namespace Aws::S3; using namespace Aws::S3::Model; int main() { // 初始化 AWS SDK Aws::SDKOptions options; Aws::InitAPI(options); // 建立 S3 客户端 Aws::Client::ClientConfiguration config; config.scheme = Aws::Http::Scheme::HTTP; config.endpointOverride = "localhost:9000"; // Minio 服务器地址和端口 config.verifySSL = false; // 关闭 SSL 验证 S3Client s3_client(config); // 上传文件到 Minio const std::string bucket_name = "my-bucket"; const std::string object_name = "my-object"; const std::string file_path = "path/to/my/file"; std::shared_ptr<std::iostream> file_stream = std::make_shared<std::fstream>(file_path.c_str(), std::ios_base::in | std::ios_base::binary); PutObjectRequest put_request; put_request.SetBucket(bucket_name); put_request.SetKey(object_name); put_request.SetBody(file_stream); auto put_outcome = s3_client.PutObject(put_request); if (put_outcome.IsSuccess()) { std::cout << "File uploaded successfully!" << std::endl; } else { std::cout << "File upload failed: " << put_outcome.GetError().GetMessage() << std::endl; } // 下载文件从 Minio const std::string downloaded_file_path = "path/to/my/downloaded/file"; GetObjectRequest get_request; get_request.SetBucket(bucket_name); get_request.SetKey(object_name); auto get_outcome = s3_client.GetObject(get_request); if (get_outcome.IsSuccess()) { std::shared_ptr<Aws::IOStream> body_stream = get_outcome.GetResult().GetBody(); Minio::ObjectReadStream object_stream(body_stream); std::ofstream downloaded_file(downloaded_file_path, std::ios_base::out | std::ios_base::binary); downloaded_file << object_stream.rdbuf(); std::cout << "File downloaded successfully!" << std::endl; } else { std::cout << "File download failed: " << get_outcome.GetError().GetMessage() << std::endl; } // 关闭 AWS SDK Aws::ShutdownAPI(options); return 0; } ``` 请注意,此示例代码假设你已经在 Minio 上创建了一个名为 "my-bucket" 的存储桶,并且已经在本地计算机上安装了 Minio 服务器。你需要根据你的实际情况进行修改。 此外,请不要忘记在代码中包含必要的头文件,并将 AWS SDK 和 Minio C++ 库添加到项目中,并在项目属性中设置正确的库和头文件路径。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值