php S3調用SDK樣本 AmazonS3

來源:互聯網
上載者:User

標籤:editor   參考   poi   .com   template   efi   -o   class   rar   

demo.php

<?php/*  * To change this license header, choose License Headers in Project Properties. * To change this template file, choose Tools | Templates * and open the template in the editor. */define(‘AWS_KEY‘, ‘input your key‘);define(‘AWS_SECRET_KEY‘, ‘input your secret key‘);$HOST = ‘input your endpoint‘;// require the amazon sdk for php libraryrequire_once dirname(__FILE__).‘/sdk.class.php‘;// Instantiate the S3 class and point it at the desired host$s3 = new AmazonS3(array(        ‘key‘ => AWS_KEY,        ‘secret‘ => AWS_SECRET_KEY,));$s3->set_hostname($HOST);$s3->allow_hostname_override(false);// Set the S3 class to use objects.dreamhost.com/bucket// instead of bucket.objects.dreamhost.com$s3->enable_path_style();$bucketname="test_wx";#=========================create_bucket====================echo str_repeat("=", 30)."create_bucket".str_repeat("=", 30)."\n";$ret=$s3->create_bucket($bucketname, AmazonS3::REGION_US_E1);echo print_r($ret,1),"\n";#=========================list_buckets====================echo str_repeat("=", 30)."list_buckets".str_repeat("=", 30)."\n";$ListResponse = $s3->list_buckets();$Buckets = $ListResponse->body->Buckets->Bucket;foreach ($Buckets as $Bucket) {        echo $Bucket->Name . "\t" . $Bucket->CreationDate . "\n";}#=========================create_object====================echo str_repeat("=", 30)."create_object:".$bucketname.str_repeat("=", 30)."\n";$s3->create_object($bucketname, ‘hello.txt‘, array(        ‘body‘ => "Hello World!",        ‘acl‘=>AmazonS3::ACL_PUBLIC,#對對象進行許可權分配));echo "create file hello.txt and assign public authority to it \n";$ret=$s3->create_object($bucketname, ‘upload.jpg‘, array(        ‘fileUpload‘ => dirname(__FILE__)."/j01.png",        ‘acl‘=>AmazonS3::ACL_PUBLIC,#對對象進行許可權分配));echo "return url:".$ret->header[‘_info‘][‘url‘],"\n";echo "upload file j01.png and assign public authority to it \n";#=========================set_object_acl  對對象進行許可權更改====================echo str_repeat("=", 30)."set_object_acl:".$bucketname.str_repeat("=", 30)."\n";$s3->set_object_acl($bucketname,"hello.txt", AmazonS3::ACL_PRIVATE);echo "change file hello.txt authority  \n";#=========================list_objects====================$ObjectsListResponse = $s3->list_objects($bucketname);$Objects = $ObjectsListResponse->body->Contents;echo str_repeat("=", 30)."list_objects:".$bucketname.str_repeat("=", 30)."\n";foreach ($Objects as $Object) {        echo $Object->Key . "\t" . $Object->Size . "\t" . $Object->LastModified . "\n";}#=========================get_object_url  擷取url====================echo str_repeat("=", 30)."get_object_url:".$bucketname.str_repeat("=", 30)."\n";$secret_url = $s3->get_object_url($bucketname, ‘j02.jpg‘, ‘1 hour‘);echo $secret_url . "\n";#=========================DOWNLOAD AN OBJECT====================#This downloads the object upload.jpg and saves it in d:/$FileHandle = fopen(‘d:/upload.jpg‘, ‘w+‘);$s3->get_object($bucketname, ‘upload.jpg‘, array(        ‘fileDownload‘ => $FileHandle,));#=========================delete_object====================echo str_repeat("=", 30)."delete_object".str_repeat("=", 30)."\n";$s3->delete_object($bucketname, ‘hello.txt‘);#=========================delete_bucket====================echo str_repeat("=", 30)."delete_bucket".str_repeat("=", 30)."\n";$ret=$s3->delete_bucket($bucketname, 1); #This will delete the bucket even if it is not empty.echo print_r($ret,1),"\n";

sdk.class.php library:

 https://github.com/amazonwebservices/aws-sdk-for-php

參考地址:

http://docs.ceph.com/docs/master/radosgw/s3/php/#change-an-object-s-acl

 

php S3調用SDK樣本 AmazonS3

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.