简体   繁体   中英

How do we upload a file to digitial ocean spaces using PHP Codeigniter?

I need to upload a file to digital ocean spaces server using a PHP codeigniter project. If anyone have a working sample please share.

As far as I know DigitalOcean's Spaces provide cloud storage for static content for your sites and it provides a CDN service too. I know that you can use their API to manipulate the buckets, may be they have API to upload files too. A simple Googling got me this link https://www.digitalocean.com/docs/spaces/resources/ You can use php cURL to upload files using their API.

You can use SPACES-API from github

composer require sociallydev/spaces-api:dev-master

Connect

//Either:
require_once("spaces.php");
//OR COMPOSER:
require_once("vendor/autoload.php"); //Install first by executing: composer require SociallyDev/Spaces-API in your project's directory.

$key = "EXAMPLE_KEY";
$secret = "EXAMPLE_SECRET";

$space_name = "my-space";
$region = "nyc3";

$space = new SpacesConnect($key, $secret, $space_name, $region);

Uploading

$path_to_file = "image.png";

$space->UploadFile($path_to_file, "public", "save_as.png");

For more clarification use this link

Don't use the spacesAPI, it's not complete.

Use the AWS SDK

use Aws\S3\S3Client;

        $this->client  = new S3Client([
                'version' => 'latest',
                'region'  => 'region',
                'endpoint' => 'https://region.digitaloceanspaces.com',
                'credentials' => [
                        'key'    => $mod->key,
                        'secret' => $mod->secret,
                 ]
       ])

Region is where you space is. For DO it apparently doesn't matter or increase speed anyway. Then you can upload the file directly with

$result = $this->client->putObject(array(
    'Bucket'     => $bucket,
    'Key'        => 'data_from_file.txt',
    'SourceFile' => $pathToFile,
    'Metadata'   => array(
        'Foo' => 'abc',
        'Baz' => '123'
    )
));

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM