zoukankan      html  css  js  c++  java
  • php S3

    转载自:http://www.cnblogs.com/wangxusummer/p/6398772.html

    <?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 library
    require_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)."
    ";
    $ret=$s3->create_bucket($bucketname, AmazonS3::REGION_US_E1);
    echo print_r($ret,1),"
    ";
    #=========================list_buckets====================
    echo str_repeat("=", 30)."list_buckets".str_repeat("=", 30)."
    ";
    $ListResponse = $s3->list_buckets();
    $Buckets = $ListResponse->body->Buckets->Bucket;
    foreach ($Buckets as $Bucket) {
            echo $Bucket->Name . "	" . $Bucket->CreationDate . "
    ";
    }
    
    
    #=========================create_object====================
    echo str_repeat("=", 30)."create_object:".$bucketname.str_repeat("=", 30)."
    ";
    $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 
    ";
    $ret=$s3->create_object($bucketname, 'upload.jpg', array(
            'fileUpload' => dirname(__FILE__)."/j01.png",
            'acl'=>AmazonS3::ACL_PUBLIC,#对对象进行权限分配
    ));
    echo "return url:".$ret->header['_info']['url'],"
    ";
    echo "upload file j01.png and assign public authority to it 
    ";
    #=========================set_object_acl  对对象进行权限更改====================
    echo str_repeat("=", 30)."set_object_acl:".$bucketname.str_repeat("=", 30)."
    ";
    $s3->set_object_acl($bucketname,"hello.txt", AmazonS3::ACL_PRIVATE);
    echo "change file hello.txt authority  
    ";
    
    #=========================list_objects====================
    
    $ObjectsListResponse = $s3->list_objects($bucketname);
    $Objects = $ObjectsListResponse->body->Contents;
    echo str_repeat("=", 30)."list_objects:".$bucketname.str_repeat("=", 30)."
    ";
    foreach ($Objects as $Object) {
            echo $Object->Key . "	" . $Object->Size . "	" . $Object->LastModified . "
    ";
    }
    #=========================get_object_url  获取url====================
    echo str_repeat("=", 30)."get_object_url:".$bucketname.str_repeat("=", 30)."
    ";
    $secret_url = $s3->get_object_url($bucketname, 'j02.jpg', '1 hour');
    echo $secret_url . "
    ";
    
    
    #=========================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)."
    ";
    $s3->delete_object($bucketname, 'hello.txt');
    #=========================delete_bucket====================
    echo str_repeat("=", 30)."delete_bucket".str_repeat("=", 30)."
    ";
    $ret=$s3->delete_bucket($bucketname, 1); #This will delete the bucket even if it is not empty.
    echo print_r($ret,1),"
    ";

    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

  • 相关阅读:
    递归函数 二分查找
    内置函数
    迭代器 生成器 推导式
    装饰器函数的有用信息
    函数名 闭包 装饰器
    动态参数 命名空间
    C#_LINQ(LINQ to Entities)
    C#_MVC 自定义AuthorizeAttribute实现权限管理
    C#_MVC3之使用Authorize简单的验证登录(一)
    C#_MVC_分页update
  • 原文地址:https://www.cnblogs.com/alin-qu/p/7339242.html
Copyright © 2011-2022 走看看