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 Aws\S3\S3Client([
'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的更多相关文章

  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. POJ - 2942 Knights of the Round Table (点双联通分量+二分图判定)

    题意:有N个人要参加会议,围圈而坐,需要举手表决,所以每次会议都必须是奇数个人参加.有M对人互相讨厌,他们的座位不能相邻.问有多少人任意一场会议都不能出席. 分析:给出的M条关系是讨厌,将每个人视作点 ...

  2. javascript Date对象 之 设置时间

    之前对js的date对象总是感觉熟悉,而不愿细细深究其所以然,所以每当自己真正应用起来的时候,总会糊里糊涂的,今日花费2个小时的时间仔细钻研了一下,感觉 豁然开朗,故,以此记录,一来 供以后查阅,二来 ...

  3. hadoop 2.0安装及HA配置简述

    一.单机模式 a.配置本机到本机的免密登录 b.解压hadoop压缩包,修改hadoop.env.sh中的JAVA_HOME c.修改core-site.xml <configuration&g ...

  4. 手动创建sql数据表

    createtable  tb (    ID   int IDENTITY (1,1)     notnull, --创建列ID,并且每次新增一条记录就会加1    WokNo            ...

  5. 【Head First Servlets and JSP】笔记 25:JSTL 参考

    <%@ page contentType="text/html;charset=UTF-8" language="java" %> <%@ t ...

  6. Centos6.5安装python2.7与pip

    安装Python2.7 安装环境 [root@localhost1 ~]# cat /etc/redhat-release CentOS release 6.5 (Final) [root@local ...

  7. CodeIgniter 资料

    PHP 论坛: http://codeigniter.org.cn/forums/forum-opensource-1.html 下载 CodeIgniter 项目 的最新软件包(http://www ...

  8. 关于file_get_contents返回False的问题

    在本地测试中,使用file_get_contents获取远程服务器的资源是可以的: public function send_post($url, $post_data = null) { $post ...

  9. 用户iis可以用外网ip访问,用内网访问报错404

    如下,没有添加内网ip绑定

  10. CNI:容器网络接口

    CNI 简介 不管是 docker 还是 kubernetes,在网络方面目前都没有一个完美的.终极的.普适性的解决方案,不同的用户和企业因为各种原因会使用不同的网络方案.目前存在网络方案 flann ...