Facebook Twitter Instagram YouTube LinkedIn WhatsApp
    Facebook Twitter Instagram LinkedIn WhatsApp
    ZaveRizwanZaveRizwan
    Subscribe
    • Home
    • Features
      • Example Post
      • Typography
      • Contact
      • View All On Demos
    • Mobiles
    • Typography
    • Mobiles
      • Tech
      • Gadgets
      • Insights
    • Buy Now
    ZaveRizwanZaveRizwan
    Cloud Storage

    How to upload to Cloud Storage using php?

    zaiveeBy zaiveeFebruary 6, 2023No Comments2 Mins Read

    You can use the open source php library PHP OpenStack SDK to upload files to your push zones. The following shows how you can use it with 5centsCDN.

    1. Add PHP OpenStack SDK in your project using Composer
      • composer require php-opencloud/openstack
    2. Add Guzzle, PHP HTTP Client in your project
      • composer require guzzlehttp/guzzle
    3. Download the OpenStack RC v3 file from the OpenStack tab of the corresponding push zone.
      OpenRC
    4. Login to the OpenStack Horizon File Manager and from left menu, Identity > Users, copy the User ID
      OpenStack USerID
    5. From the OpenStack RC File copy the variables:
      • OS_PROJECT_ID, OS_USERNAME, OS_PASSWORD
    6. Create the php file and copy paste the following OpenStack initialization code
      <?php
      
      require "vendor/autoload.php";
      use GuzzleHttpPsr7Stream;
      
      $openstack = new OpenStackOpenStack([
        'authUrl' => 'http://controller.5centscdn.com/v3/',
        'region' => 'RegionOne',
        'user' => [
          'id' => 'xxxxxxxxx', // Replace with User ID copied in Step 4
          'password' => 'xxxxxxx', // Replace with OS_PASSWORD copied from RC File
        ],
        'scope' => [
          'project' => [
            'id' => 'xxxxxxx', //Replace with OS_PROJECT_ID
          ]
        ]
      ]);
      
      $container = $openstack->objectStoreV1()->getContainer('xxxxxxx'); //Replace with OS_USERNAME
    7. After you can follow the instructions in the PHP OpenStack SDK Docs to upload the files

    Sample Usage

    /* list container properties */
    $container->retrieve();
    print_r($container);
    
    /* list objects */
    foreach ($container->listObjects([], function ($o) { return $o->retrieve(); }) as $object) {
    print_r($object);
    }
    
    /* List Object Detail */
    $object = $container->getObject('01.mp4');
    $object->retrieve();
    echo "{$object->name}t{$object->contentType}t{$object->contentLength}".PHP_EOL;
    
    /* Upload files <5GB */
    $file = '/path/to/filename.ext';//Absolute path to the file
    $object = $container->createObject([
    'name' => 'raw/'.pathinfo($file, PATHINFO_BASENAME),
    'stream' => new Stream(fopen($file, 'r')),
    ]);
    $object->retrieve();
    print_r($object);

    Related Posts

    How to download files using Cyberduck?

    February 6, 2023

    How to upload files to OpenStack Swift using curl?

    February 6, 2023

    How to Login and upload files via OpenStack Horizon?

    February 6, 2023
    Add A Comment

    Leave A Reply Cancel Reply

    © 2023 ZaveRizwan. Designed by ZaveRizwan.
    • Home
    • Tech
    • Gadgets
    • Mobiles
    • Buy Now

    Type above and press Enter to search. Press Esc to cancel.