aws 云存储 Linux sdk,使用 minio 搭建私有对象存储云。aws-php-sdk 操作object

How to use AWS SDK for PHP with Minio Server

aws-sdk-php is the official AWS SDK for the PHP programming language. In this recipe we will learn how to use aws-sdk-php with Minio server.

1. Prerequisites

Install Minio Server from here.

2. Installation

Install aws-sdk-php from AWS SDK for PHP official docs here.

3. Use GetObject and PutObject

Example below shows putObject and getObject operations on Minio server using aws-sdk-php. Please replace endpoint,key, secret, Bucket with your local setup in this example.php file. Note that we set use_path_style_endpoint to true to use Minio with AWS SDK for PHP. Read more in the AWS SDK for PHP docs here.

Copy<?php

// Include the SDK using the Composer autoloader

date_default_timezone_set('America/Los_Angeles');

require 'vendor/autoload.php';

$s3 = new AwsS3S3Client([

'version' => 'latest',

'region' => 'us-east-1',

'endpoint' => 'http://localhost:9000',

'use_path_style_endpoint' => true,

'credentials' => [

'key' => 'YOUR-ACCESSKEYID',

'secret' => 'YOUR-SECRETACCESSKEY',

],

]);

// Send a PutObject request and get the result object.

$insert = $s3->putObject([

'Bucket' => 'testbucket',

'Key' => 'testkey',

'Body' => 'Hello from Minio!!'

]);

// Download the contents of the object.

$retrive = $s3->getObject([

'Bucket' => 'testbucket',

'Key' => 'testkey',

'SaveAs' => 'testkey_local'

]);

// Print the body of the result by indexing into the result object.

echo $retrive['Body'];

After the file is updated, run the program

Copyphp example.php

Hello from Minio!!

4. Create a pre-signed URL

Copy<?php

// Get a command object from the client

$command = $s3->getCommand('GetObject', [

'Bucket' => 'testbucket',

'Key' => 'testkey'

]);

// Create a pre-signed URL for a request with duration of 10 miniutes

$presignedRequest = $s3->createPresignedRequest($command, '+10 minutes');

// Get the actual presigned-url

$presignedUrl = (string) $presignedRequest->getUri();

5. Get a plain URL

To get a plain URL, you'll need to make your object/bucket accessible with public permission. Also, note that you'll not get the URL with X-Amz-Algorithm=[...]&X-Amz-Credential=[...]&X-Amz-Date=[...]&X-Amz-Expires=[...]&X-Amz-SignedHeaders=[...]&X-Amz-Signature=[...]

Copy<?php

$plainUrl = $s3->getObjectUrl('testbucket', 'testkey');

6. Set a Bucket Policy

Copy<?php

$bucket = 'testbucket';

// This policy sets the bucket to read only

$policyReadOnly = '{

"Version": "2012-10-17",

"Statement": [

{

"Action": [

"s3:GetBucketLocation",

"s3:ListBucket"

],

"Effect": "Allow",

"Principal": {

"AWS": [

"*"

]

},

"Resource": [

"arn:aws:s3:::%s"

],

"Sid": ""

},

{

"Action": [

"s3:GetObject"

],

"Effect": "Allow",

"Principal": {

"AWS": [

"*"

]

},

"Resource": [

"arn:aws:s3:::%s/*"

],

"Sid": ""

}

]

}

';

// If you want to put it on a specific folder you just change 'arn:aws:s3:::%s/*' to 'arn:aws:s3:::%s/folder/*'

// Create a bucket

$result = $s3->createBucket([

'Bucket' => $bucket,

]);

// Configure the policy

$s3->putBucketPolicy([

'Bucket' => $bucket,

'Policy' => sprintf($policyReadOnly, $bucket, $bucket),

]);

获取对象的方法:

$result = $client->getObject(array(

'Bucket'                     => '123124',

'Key'                        => '2018022610202562513.jpeg',

'ResponseContentType'        => 'image/jpeg',

// 'ResponseContentLanguage'    => 'en-US',

// 'ResponseContentDisposition' => 'attachment; filename=testing.txt',

// 'ResponseCacheControl'       => 'No-cache',

// 'ResponseExpires'            => gmdate(DATE_RFC2822, time() + 3600),

));

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值