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

摘要:
']);// 下载对象的内容。$retrive=$s3-˃getObject([“Bucket”=˃“testbucket”,“Key”=˃“estkey”,“SaveAs”=˃“testkey_local”]);//通过索引结果对象来打印结果的正文。echo$retrieve['Body'];完成后,运行程序Copyphpexample.php HellofromMinio!!4.创建已签名的URLCopygetCommand('GetObject',['Bucket'=˃'testbucket','Key'=˃'estkey']);//为持续时间为10分钟的请求创建预签名URL$presignedRequest=$s3-˃createPresignedRequest($command,“+10分钟”);//获取实际辞职url$presignedUrl=(字符串)$presigned请求-˃getUri();5.使用URL获取URL时,您需要使用公共权限访问您的对象/Bucket。此外,请注意,您不能使用X-Amz-Algorithm=[…]&X-Amz-Credential=[…]&X-Amz Date=[…][X-Amz-Expires=[…][X-Amz-SignedHeaders=[…]和X-Amz-Signature=[…]Copy˂?
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),
            ));


免责声明:文章转载自《使用 minio 搭建私有对象存储云。aws-php-sdk 操作object》仅用于学习参考。如对内容有疑问,请及时联系本站处理。

上篇vue 多个audio播放 一个audio播放其他audio禁止播放wampserver修改mysql数据库密码的简单方式下篇

宿迁高防,2C2G15M,22元/月;香港BGP,2C5G5M,25元/月 雨云优惠码:MjYwNzM=

相关文章

再探NSString

再探NSString NSString应该是oc开发中最常用的一个数据类型了,这次对该类型再进行一次全方位的探索与总结。 NSString本质上属于OC类对象,继承于NSObject,遵守NSCopying, NSMutableCopying, NSSecureCoding协议。 NSMutableString与之类似,唯一不同的是它继承于NSStrin...

Spring框架之jdbc源码完全解析

Spring框架之jdbc源码完全解析        Spring JDBC抽象框架所带来的价值将在以下几个方面得以体现:        1、指定数据库连接参数        2、打开数据库连接        3、声明SQL语句        4、预编译并执行SQL语句        5、遍历查询结果(如果需要的话)        6、处理每一次遍历操作...

ASP.NET 中实现会话状态的基础

简介 在 Web 应用程序这样的无状态环境中,了解会话状态的概念并没有实际的意义。尽管如此,有效的状态管理对于大多数 Web 应用程序来说都是一个必备的功能。Microsoft® ASP.NET 以及许多其他服务器端编程环境都提供了一个抽象层,允许应用程序基于每个用户和每个应用程序存储持久性数据。 需要特别注意的是,Web 应用程序的会话状态是应用程序在不...

JavaWeb之Cookie和Session的区别

Cookie和Session的区别 一、cookie机制和session机制的区别 ************************************************************************************* 具体来说cookie机制采用的是在客户端保持状态的方案,而session机制采用的是在服务器端保持状态...

数据存储

iOS应用数据存储的常用方式XML属性列表(plist)归档Preference(偏好设置)NSKeyedArchiver归档(NSCoding)SQLite3Core Data应用沙盒每个iOS应用都有自己的应用沙盒(应用沙盒就是文件系统目录),与其他文件系统隔离。应用必须待在自己的沙盒里,其他应用不能访问该沙盒应用沙盒的文件系统目录,如下图所示(假设应...

java容器类

一、  容器类: 下图摘自《Java编程思想》,很好地展示了整个容器类的结构。     由上图可知,容器类库可分为两大类,各自实现了Collection接口和Map接口,下面就常见的类进行一下分类: 实现Collection接口的容器类 Collection  ├List  │├LinkedList  │├ArrayList  │└Vector  │ └S...