IBM cloud storage with PHP

442 views Asked by At

I am trying to create bucket and upload files on IBM cloud storage via PHP, but getting error 405 Method not allowed, i have read the docs it mentioned that we can use s3 so i am using AWS s3 sdk to connect but its not working.

<?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:*******************:*******************::"
  }

So this is the code which i have to used to authenticate and upload the files to bucket but its not working.

1

There are 1 answers

0
Akhilesh On BEST ANSWER

I finally make it working, now files and folders are uploading to IBM correctly. Just posting this answer so that anyone facing issue can see this.

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());
    }
}