PHP 的 IBM 云存储

IBM cloud storage with PHP

我正在尝试通过 PHP 在 IBM 云存储上创建存储桶和上传文件,但收到错误 405 方法不允许,我已经阅读了它提到我们可以使用 s3 的文档,所以我正在使用 AWS s3 sdk 连接但它不工作。

<?php

define('IBM_KEY', '******************************');
define('IBM_SECRET', '************************');
define('IBM_VERSION', 'latest');
define('IBM_REGION', 'au-syd');

function get_ibm_client($bucket_name)
{
    $endpoint = "https://{$bucket_name}.s3-web." . IBM_REGION . ".cloud-object-storage.appdomain.cloud/";
    

    return new Aws\S3\S3Client(array(
        'credentials' => [
            'key' => IBM_KEY,
            'secret' => IBM_SECRET,
        ],
        'version' => 'latest',
        'region' => IBM_REGION,
        'bucket_endpoint' => true,
        'endpoint' => $endpoint
    ));
}

function upload_file_ibm($bucket_name, $folder_name = '', $file_name)
{
    if (empty(trim($bucket_name))) {
        return array('success' => false, 'message' => 'Please provide valid bucket name!');
    }

    if (empty(trim($file_name))) {
        return array('success' => false, 'message' => 'Please provide valid file name!');
    }

    if ($folder_name !== '') {
        $keyname = $folder_name . '/' . $file_name;
    } else {
        $keyname = $file_name;
    }

    $s3 = get_ibm_client($bucket_name);

    $file_url = 'https://' . $bucket_name . '.s3-web.' . IBM_REGION .'.cloud-object-storage.appdomain.cloud/'. $keyname;

    try {
        $s3->putObject(array(
            'Bucket' => $bucket_name,
            'Key' => $keyname,
            'SourceFile' => $file_name,
            'StorageClass' => 'REDUCED_REDUNDANCY'
        ));

        return array('success' => true, 'message' => $file_url);
    } catch (S3Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    } catch (Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    }
}

These are the keys 

{
    "apikey": "*******************",
    "cos_hmac_keys": {
      "access_key_id": "*******************", // this is used above
      "secret_access_key": "*******************" // this is used above
    },
    "endpoints": "https://control.cloud-object-storage.cloud.ibm.com/v2/endpoints",
    "iam_apikey_description": "Auto-generated for key *******************",
    "iam_apikey_name": "Service credentials-1",
    "iam_role_crn": "crn:v1:bluemix:public:iam::::serviceRole:Manager",
    "iam_serviceid_crn": "crn:v1:bluemix:public:iam-identity::*******************::serviceid:*******************",
    "resource_instance_id": "crn:v1:bluemix:public:cloud-object-storage:global:*******************:*******************::"
  }

所以这是我必须用来验证文件并将文件上传到存储桶的代码,但它不起作用。

我终于让它工作了,现在文件和文件夹正在正确上传到 IBM。只是发布这个答案,以便任何遇到问题的人都能看到。

function get_ibm_client($bucket_name)
{
    $endpoint = "https://{$bucket_name}.s3." . IBM_REGION . ".cloud-object-storage.appdomain.cloud/";

    return new Aws\S3\S3Client(array(
        'credentials' => [
            'key' => IBM_KEY,
            'secret' => IBM_SECRET,
        ],
        'version' => 'latest',
        'region' => IBM_REGION,
        'bucket_endpoint' => true,
        'endpoint' => $endpoint
    ));
}

function upload_file_ibm($bucket_name, $folder_name = '', $file_name)
{
    if (empty(trim($bucket_name))) {
        return array('success' => false, 'message' => 'Please provide valid bucket name!');
    }

    if (empty(trim($file_name))) {
        return array('success' => false, 'message' => 'Please provide valid file name!');
    }

    if ($folder_name !== '') {
        $keyname = $folder_name . '/' . $file_name;
    } else {
        $keyname = $file_name;
    }

    $s3 = get_ibm_client($bucket_name);

    $file_url = 'https://' . $bucket_name . '.s3.' . IBM_REGION .'.cloud-object-storage.appdomain.cloud/'. $keyname;

    try {
        $s3->putObject(array(
            'Bucket' => $bucket_name,
            'Key' => $keyname,
            'SourceFile' => $file_name,
            'StorageClass' => 'REDUCED_REDUNDANCY'
        ));

        return array('success' => true, 'message' => $file_url);
    } catch (S3Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    } catch (Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    }
}

function upload_folder_ibm($bucket_name, $folder_name)
{

    if (empty(trim($bucket_name))) {
        return array('success' => false, 'message' => 'Please provide valid bucket name!');
    }

    if (empty(trim($folder_name))) {
        return array('success' => false, 'message' => 'Please provide valid folder name!');
    }

    $keyname = $folder_name;

    $s3 = get_ibm_client($bucket_name);

    try {
        $manager = new \Aws\S3\Transfer($s3, $keyname, 's3://' . $bucket_name);
        $manager->transfer();
        return array('success' => true);
    } catch (S3Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    } catch (Exception $e) {
        return array('success' => false, 'message' => $e->getMessage());
    }
}