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. poj2993

    #include<stdio.h> #include<string.h> #include<algorithm> using namespace std; stru ...

  2. jenkins添加GIT repository报错

    添加了ssh互信,但一直提示如下错误. Failed to connect to repository : Command "git ls-remote -h git@git.xxx.cn: ...

  3. 饭卡管理系统学生E-R图

    - 2 - 1.1 可行性分析 1.1.1 项目背景 近年来学生食堂饭卡的使用给高校餐饮管理带来了一次革命, 从结算方式到账户管理, 从卫生便捷到数据统计等,不仅给就餐者带来了方便,也使餐饮结算手段发 ...

  4. Linux系统基本的内存管理知识讲解

    内存是Linux内核所管理的最重要的资源之一.内存管理系统是操作系统中最为重要的部分,因为系统的物理内存总是少于系统所需要的内存数量.虚拟内存就是为了克服这个矛盾而采用的策略.系统的虚拟内存通过在各个 ...

  5. struts2标签库详解

    要在jsp中使用Struts2的标志,先要指明标志的引入.通过jsp的代码的顶部加入以下的代码: <%@taglib prefix="s" uri="/struts ...

  6. Contest-hunter 暑假送温暖 SRM08

    01-07都没写...然后突然来写貌似有点突兀啊...不管了,难得前排记录一下... 吐槽一下赛制...不得不说很强... cf 套oi...很创新...不过还是兹磁ACM或者CF A-1 数据才2& ...

  7. tomcat深入学习—权限篇

    如果想给一个应用程序配置权限,可能需要的代码量很多,但你知道吗,仅通过配置tomcat,就能有同样的效果(为一个web应用添加了权限) 效果:打开http://localhost:8080/searc ...

  8. postman(谷歌) httprequester(火狐)

    http://www.cnblogs.com/s380774061/p/4624326.html @an http://www.tuicool.com/articles/67Rnaej 测试文档券栈 ...

  9. JavaWeb -- Struts2,对比, 简单表单提交,校验,防重复提交, 文件上传

    Struts2核心流程图 1. Struts2 和 Struts1 对比 struts1:基于Servlet(ActionServlet),actionForm众多(类的爆炸),action单例(数据 ...

  10. 关于Jupyter Notebook默认起始目录设置无效的解决方法

    一.问题描述 今天折腾jupyter的时候,突然觉得起始目录是用户根目录很麻烦,想着把他改成自己的某个文件,按照网上方法折腾半天也还是无效.东点点西看看才发现端倪. [win10以下好像没这个问题,修 ...