PHP S3 Invoke SDK Example AmazonS3

Source: Internet
Author: User

demo.php

<?PHP/** To change the license header, choose License Headers in the Project Properties. * To change this template file, Choos E 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$s 3=NewAmazonS3 (Array(        ' Key ' = Aws_key, ' secret ' = Aws_secret_key,));$s 3->set_hostname ($HOST);$s 3->allow_hostname_override (false);//Set the S3 class to use objects.dreamhost.com/bucket//instead of bucket.objects.dreamhost.com$s 3-Enable_path_style ();$bucketname= "TEST_WX";#=========================create_bucket====================Echo str_repeat("=", 30). " Create_bucket ".str_repeat("=", 30). " \ n ";$ret=$s 3->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=$s 3-list_buckets ();$Buckets=$ListResponse->body->buckets->buckets;foreach($Buckets  as $Bucket) {        Echo $Bucket->name. "\ T".$Bucket->creationdate. "\ n";}#=========================create_object====================Echo str_repeat("=", 30). " Create_object: ".$bucketname.str_repeat("=", 30). " \ n ";$s 3->create_object ($bucketname, ' Hello.txt ',Array(        ' Body ' = ' Hello world! ', ' ACL ' =>amazons3::acl_public,#assigning permissions to an object));Echo"Create file Hello.txt and assign public authority to it \ n";$ret=$s 3->create_object ($bucketname, ' upload.jpg ',Array(        ' FileUpload ' =dirname(__file__)." /j01.png ", ' ACL ' =>amazons3::acl_public,#assigning permissions to an object));Echo"Return URL:".$ret-Header[' _info '] [' url '], ' \ n ';Echo"Upload file J01.png and assign public authority to it \ n";#=========================set_object_acl permission changes to an object ====================Echo str_repeat("=", 30). " Set_object_acl: ".$bucketname.str_repeat("=", 30). " \ n ";$s 3->set_object_acl ($bucketname, "Hello.txt", AmazonS3::acl_private);Echo"Change file Hello.txt authority \ n";#=========================list_objects====================$ObjectsListResponse=$s 3->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 Get url====================Echo str_repeat("=", 30). " Get_object_url: ".$bucketname.str_repeat("=", 30). " \ n ";$secret _url=$s 3->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+ ');$s 3->get_object ($bucketname, ' upload.jpg ',Array(        ' FileDownload ' =$FileHandle,));#=========================delete_object====================Echo str_repeat("=", 30). " Delete_object ".str_repeat("=", 30). " \ n ";$s 3->delete_object ($bucketname, ' Hello.txt ');#=========================delete_bucket====================Echo str_repeat("=", 30). " Delete_bucket ".str_repeat("=", 30). " \ n ";$ret=$s 3->delete_bucket ($bucketname, 1);#This would 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

Reference Address:

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

PHP S3 Invoke SDK Example AmazonS3

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.