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.

  1. Copy<?php
  2. // Include the SDK using the Composer autoloader
  3. date_default_timezone_set('America/Los_Angeles');
  4. require 'vendor/autoload.php';
  5. $s3 = new Aws\S3\S3Client([
  6. 'version' => 'latest',
  7. 'region' => 'us-east-1',
  8. 'endpoint' => 'http://localhost:9000',
  9. 'use_path_style_endpoint' => true,
  10. 'credentials' => [
  11. 'key' => 'YOUR-ACCESSKEYID',
  12. 'secret' => 'YOUR-SECRETACCESSKEY',
  13. ],
  14. ]);
  15. // Send a PutObject request and get the result object.
  16. $insert = $s3->putObject([
  17. 'Bucket' => 'testbucket',
  18. 'Key' => 'testkey',
  19. 'Body' => 'Hello from Minio!!'
  20. ]);
  21. // Download the contents of the object.
  22. $retrive = $s3->getObject([
  23. 'Bucket' => 'testbucket',
  24. 'Key' => 'testkey',
  25. 'SaveAs' => 'testkey_local'
  26. ]);
  27. // Print the body of the result by indexing into the result object.
  28. echo $retrive['Body'];

After the file is updated, run the program

  1. Copyphp example.php
  2. Hello from Minio!!

4. Create a pre-signed URL

  1. Copy<?php
  2. // Get a command object from the client
  3. $command = $s3->getCommand('GetObject', [
  4. 'Bucket' => 'testbucket',
  5. 'Key' => 'testkey'
  6. ]);
  7. // Create a pre-signed URL for a request with duration of 10 miniutes
  8. $presignedRequest = $s3->createPresignedRequest($command, '+10 minutes');
  9. // Get the actual presigned-url
  10. $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=[...]

  1. Copy<?php
  2. $plainUrl = $s3->getObjectUrl('testbucket', 'testkey');

6. Set a Bucket Policy

  1. Copy<?php
  2. $bucket = 'testbucket';
  3. // This policy sets the bucket to read only
  4. $policyReadOnly = '{
  5. "Version": "2012-10-17",
  6. "Statement": [
  7. {
  8. "Action": [
  9. "s3:GetBucketLocation",
  10. "s3:ListBucket"
  11. ],
  12. "Effect": "Allow",
  13. "Principal": {
  14. "AWS": [
  15. "*"
  16. ]
  17. },
  18. "Resource": [
  19. "arn:aws:s3:::%s"
  20. ],
  21. "Sid": ""
  22. },
  23. {
  24. "Action": [
  25. "s3:GetObject"
  26. ],
  27. "Effect": "Allow",
  28. "Principal": {
  29. "AWS": [
  30. "*"
  31. ]
  32. },
  33. "Resource": [
  34. "arn:aws:s3:::%s/*"
  35. ],
  36. "Sid": ""
  37. }
  38. ]
  39. }
  40. ';
  41. // If you want to put it on a specific folder you just change 'arn:aws:s3:::%s/*' to 'arn:aws:s3:::%s/folder/*'
  42. // Create a bucket
  43. $result = $s3->createBucket([
  44. 'Bucket' => $bucket,
  45. ]);
  46. // Configure the policy
  47. $s3->putBucketPolicy([
  48. 'Bucket' => $bucket,
  49. 'Policy' => sprintf($policyReadOnly, $bucket, $bucket),
  50. ]);
  51. 获取对象的方法:
                $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的更多相关文章

  1. 使用CentOS8搭建私有NAS存储的一些建议

    对于超过2TB的硬盘来说只能考虑GPT分区表,因此还是建议使用EFI来安装系统. 对于超过2TB的硬盘来说应该选择LVM,然后磁盘末尾预留出至少100G的空间用于将来方便维护安装个Windows系统之 ...

  2. 使用MinIO搭建对象存储服务

    1.MinIO是什么? MinIO 是一款高性能.分布式的对象存储系统. 它是一款软件产品, 可以100%的运行在标准硬件.即X86等低成本机器也能够很好的运行MinIO. MinIO与传统的存储和其 ...

  3. 干货 | 基于Go SDK操作京东云对象存储OSS的入门指南

    前言 本文介绍如何使用Go语言对京东云对象存储OSS进行基本的操作,帮助客户快速通过Go SDK接入京东云对象存储,提高应用开发的效率. 在实际操作之前,我们先看一下京东云OSS的API接口支持范围和 ...

  4. 阿里云对象存储OSS访问控制

    阿里云对象存储OSS的Android SDK提供了STS鉴权模式和自签名模式来保障移动终端的安全性. OSS可以通过阿里云STS (Security Token Service) 进行临时授权访问.交 ...

  5. openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 四

    openstack-r版(rocky)搭建基于centos7.4 的openstack swift对象存储服务 一 openstack-r版(rocky)搭建基于centos7.4 的openstac ...

  6. 听说你想把对象存储当 HDFS 用,我们这里有个方案...

    传统的大数据集群往往采用本地中心化的计算和存储集群.比如在谷歌早期的[三驾马车]中,使用 GFS 进行海量网页数据存储,用 BigTable 作为数据库并为上层提供各种数据发现的能力,同时用 MapR ...

  7. ceph 对象存储跨机房容灾

    场景分析 每个机房的Ceph都是独立的cluster,彼此之间没有任何关系. 多个机房都独立的提供对象存储功能,每个Ceph Radosgw都有自己独立的命名空间和存储空间. 这样带来两个问题: 针对 ...

  8. Docsify+腾讯云对象存储 COS,一键搭建云上静态博客

    最近一直在想如何利用 COS 简化静态博客的搭建过程.搜了很多的静态博客搭建过程,发现大部分的静态博客都要通过编译才能生成静态页面.功夫不负有心人,终于让我找到了一个超简洁博客的搭建方法. 效果预览 ...

  9. 阿里云对象存储 OSS 应用服务器搭建代码

    背景说明 最近做一个APP客户端图片直传阿里云OSS的服务,需要在后台开一个阿里云的OSSToken获取的接口. 阿里云官方文档地址:快速搭建移动应用直传服务. 略过移动端说明,直接看服务端的. 不是 ...

随机推荐

  1. $命令行参数解析模块argparse的用法

    argparse是python内置的命令行参数解析模块,可以用来为程序配置功能丰富的命令行参数,方便使用,本文总结一下其基本用法. 测试脚本 把以下脚本存在argtest.py文件中: # codin ...

  2. 左连接、右连接、内连接和where

    首先可以看下w3school写的关于join的介绍: http://www.w3school.com.cn/sql/sql_join.asp on是关联条件,where是筛选条件 数据库在通过连接两张 ...

  3. 对于C++指针的详细理解

    (1)每一个变量都有一个内存位置,每一个内存位置都定义了可使用连字号(&)运算符访问的地址,它表示了在内存中的一个地址.  eg: int var1; &var1  表示var1的地址 ...

  4. MapReduce:输出是一个文本文件,每一行第一个数字式行标,第二个数字是输入文件中每一行除行标外数字的平均值,且整数不保留小数,小数保留两位小数点

    有时候你会遇到这样的问题:你有一个表格,给出了每个人在十二月,一月和二月的收入. 表格如下: 姓名 一月 二月 三月 楚乔     200   314   3500 宇文玥     2000  332 ...

  5. C++类初始化列表

    转自:https://www.cnblogs.com/BlueTzar/articles/1223169.html 构造函数初始化列表以一个冒号开始,接着是以逗号分隔的数据成员列表,每个数据成员后面跟 ...

  6. 源码编译配置lnmp部署zabbix

    环境说明: [root@wcy ~]# cat /etc/redhat-release CentOS release 6.9 (Final) [root@wcy ~]# uname -a Linux ...

  7. COS-6主存管理

    操作系统是用户和计算机的接口,同时也是计算机硬件和其他软件的接口.操作系统的功能包括管理计算机系统的硬件.软件及数据资源,控制程序运行,改善人机界面,为其它应用软件提供支持,让计算机系统所有资源最大限 ...

  8. Github fork其他项目的分支与主干保持同步

    Fork一个Repo Fork是一个复制的操作,当你Fork一个项目之后,你就有了在原项目的基础之上进行修改和扩展的权限. 通常情况下,Fork操作用于参与别人的项目(成为项目中的一员),或者以别人的 ...

  9. Spring_通过注解配置 Bean(1)

    beans-annotation.xml <?xml version="1.0" encoding="UTF-8"?><beans xmlns ...

  10. [POI2001]和平委员会

    题目描述 根据宪法,Byteland民主共和国的公众和平委员会应该在国会中通过立法程序来创立. 不幸的是,由于某些党派代表之间的不和睦而使得这件事存在障碍. 此委员会必须满足下列条件: 每个党派都在委 ...