• 沒有找到結果。

● 您需要规划桶所在的区域信息,并根据区域确定调用API的Endpoint,详细信息请 参见地区和终端节点。

区域一旦确定,创建完成后无法修改。

cn-north-4 区域创建一个名为 bucket001 的桶

示例中使用通用的Apache Http Client。

package com.obsclient;

import java.io.*;

import org.apache.http.Header;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

public class TestMain {

public static String accessKey = "UDSIAMSTUBTEST000012"; //取值为获取的AK

public static String securityKey = "Udsiamstubtest000000UDSIAMSTUBTEST000012"; //取值为获取的SK public static String region = "cn-north-4"; // 取值为规划桶所在的区域

public static String createBucketTemplate = "<CreateBucketConfiguration " +

"xmlns=\"http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/\">\n" + "<Location>" + region + "</Location>\n" +

"</CreateBucketConfiguration>";

public static void main(String[] str) { createBucket();

}

private static void createBucket() {

CloseableHttpClient httpClient = HttpClients.createDefault();

String requesttime = DateUtils.formatDate(System.currentTimeMillis());

String contentType = "application/xml";

HttpPut httpPut = new HttpPut("http://bucket001.obs.cn-north-4.myhuaweicloud.com");

httpPut.addHeader("Date", requesttime);

httpPut.addHeader("Content-Type", contentType);

/** 根据请求计算签名**/

String contentMD5 = "";

String canonicalizedHeaders = "";

String canonicalizedResource = "/bucket001/";

// Content-MD5 、Content-Type 没有直接换行, data格式为RFC 1123,和请求中的时间一致

String canonicalString = "PUT" + "\n" + contentMD5 + "\n" + contentType + "\n" + requesttime + "\n"

+ canonicalizedHeaders + canonicalizedResource;

System.out.println("StringToSign:[" + canonicalString + "]");

String signature = null;

CloseableHttpResponse httpResponse = null;

try {

signature = Signature.signWithHmacSha1(securityKey, canonicalString);

// 增加签名头域 Authorization: OBS AccessKeyID:signature

httpPut.addHeader("Authorization", "OBS " + accessKey + ":" + signature);

// 增加body体

httpPut.setEntity(new StringEntity(createBucketTemplate));

httpResponse = httpClient.execute(httpPut);

System.out.println(httpResponse.getStatusLine());

for (Header header : httpResponse.getAllHeaders()) {

System.out.println(header.getName() + ":" + header.getValue());

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

httpResponse.getEntity().getContent()));

System.out.println(response.toString());

package com.obsclient;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Locale;

import java.util.TimeZone;

public class DateUtils {

public static String formatDate(long time) {

DateFormat serverDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);

serverDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

return serverDateFormat.format(time);

} }

签名字符串Signature的计算方法为:

package com.obsclient;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;

import java.security.InvalidKeyException;

import java.util.Base64;

public class Signature {

public static String signWithHmacSha1(String sk, String canonicalString) throws UnsupportedEncodingException {

try {

SecretKeySpec signingKey = new SecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1");

Mac mac = Mac.getInstance("HmacSHA1");

mac.init(signingKey);

return Base64.getEncoder().encodeToString(mac.doFinal(canonicalString.getBytes("UTF-8")));

} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { e.printStackTrace();

}

return null;

} }

4.2 获取桶列表

操作场景

如果用户想要查看自己创建的所有桶信息,可以使用获取桶列表接口查看。

下面介绍如何调用获取桶列表API,API的调用方法请参见3 如何调用API。

前提条件

● 已获取AK和SK,获取方法参见7.3 获取访问密钥(AK/SK)。

● 您需要明确需要列举的桶所在的区域信息,并根据区域确定调用API的Endpoint,

详细信息请参见地区和终端节点。

获取 cn-north-4 区域的桶列表

示例中使用通用的Apache Http Client。

package com.obsclient;

import java.io.*;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.entity.InputStreamEntity;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

public class TestMain {

public static String accessKey = "UDSIAMSTUBTEST000012"; //取值为获取的AK

public static String securityKey = "Udsiamstubtest000000UDSIAMSTUBTEST000012"; //取值为获取的SK public static void main(String[] str) {

listAllMyBuckets();

}

private static void listAllMyBuckets() {

CloseableHttpClient httpClient = HttpClients.createDefault();

String requesttime = DateUtils.formatDate(System.currentTimeMillis());

HttpGet httpGet = new HttpGet("http://obs.cn-north-4.myhuaweicloud.com");

httpGet.addHeader("Date", requesttime);

/** 根据请求计算签名**/

String contentMD5 = "";

String contentType = "";

String canonicalizedHeaders = "";

String canonicalizedResource = "/";

// Content-MD5 、Content-Type 没有直接换行, data格式为RFC 1123,和请求中的时间一致

String canonicalString = "GET" + "\n" + contentMD5 + "\n" + contentType + "\n" + requesttime + "\n"

+ canonicalizedHeaders + canonicalizedResource;

System.out.println("StringToSign:[" + canonicalString + "]");

String signature = null;

try {

signature = Signature.signWithHmacSha1(securityKey, canonicalString);

// 增加签名头域 Authorization: OBS AccessKeyID:signature

httpGet.addHeader("Authorization", "OBS " + accessKey + ":" + signature);

CloseableHttpResponse httpResponse = httpClient.execute(httpGet);

// 打印发送请求信息和收到的响应消息

System.out.println(httpResponse.getStatusLine());

for (Header header : httpResponse.getAllHeaders()) {

System.out.println(header.getName() + ":" + header.getValue());

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

httpResponse.getEntity().getContent()));

package com.obsclient;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Locale;

import java.util.TimeZone;

public class DateUtils {

public static String formatDate(long time) {

DateFormat serverDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z",

Locale.ENGLISH);

serverDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

return serverDateFormat.format(time);

} }

签名字符串Signature的计算方法为:

package com.obsclient;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;

import java.security.InvalidKeyException;

import java.util.Base64;

public class Signature {

public static String signWithHmacSha1(String sk, String canonicalString) throws UnsupportedEncodingException {

try {

SecretKeySpec signingKey = new SecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1");

Mac mac = Mac.getInstance("HmacSHA1");

mac.init(signingKey);

return Base64.getEncoder().encodeToString(mac.doFinal(canonicalString.getBytes("UTF-8")));

} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { e.printStackTrace();

}

return null;

} }

4.3 上传对象

操作场景

您可以根据需要,将任何类型的文件上传到OBS桶中进行存储。

下面介绍如何调用PUT上传API在指定的桶中上传对象,API的调用方法请参见3 如何 调用API。

前提条件

● 已获取AK和SK,获取方法参见7.3 获取访问密钥(AK/SK)。

● 已创建了至少一个可用的桶。

● 已准备好了待上传的文件,并清楚文件所在的本地完整路径。

● 您需要知道待上传桶所在的区域信息,并根据区域确定调用API的Endpoint,详细 信息请参见地区和终端节点。

cn-north-4 区域的桶 bucket001 中上传对象,名称为 objecttest1

示例中使用通用的Apache Http Client。

package com.obsclient;

import java.io.*;

import java.util.ArrayList;

import java.util.List;

import org.apache.http.Header;

import org.apache.http.HttpEntity;

import org.apache.http.NameValuePair;

import org.apache.http.client.entity.UrlEncodedFormEntity;

import org.apache.http.client.methods.CloseableHttpResponse;

import org.apache.http.client.methods.HttpGet;

import org.apache.http.client.methods.HttpPost;

import org.apache.http.client.methods.HttpPut;

import org.apache.http.entity.InputStreamEntity;

import org.apache.http.entity.StringEntity;

import org.apache.http.impl.client.CloseableHttpClient;

import org.apache.http.impl.client.HttpClients;

import org.apache.http.message.BasicNameValuePair;

public class TestMain {

public static String accessKey = "UDSIAMSTUBTEST000012"; //取值为获取的AK

public static String securityKey = "Udsiamstubtest000000UDSIAMSTUBTEST000012"; //取值为获取的SK public static void main(String[] str) {

putObjectToBucket();

}

private static void putObjectToBucket() { InputStream inputStream = null;

CloseableHttpClient httpClient = HttpClients.createDefault();

CloseableHttpResponse httpResponse = null;

String requestTime = DateUtils.formatDate(System.currentTimeMillis());

HttpPut httpPut = new HttpPut("http://bucket001.obs.cn-north-4.myhuaweicloud.com/objecttest1");

httpPut.addHeader("Date", requestTime);

/** 根据请求计算签名 **/

String contentMD5 = "";

String contentType = "";

String canonicalizedHeaders = "";

String canonicalizedResource = "/bucket001/objecttest1";

// Content-MD5 、Content-Type 没有直接换行, data格式为RFC 1123,和请求中的时间一致

String canonicalString = "PUT" + "\n" + contentMD5 + "\n" + contentType + "\n" + requestTime + "\n"

+ canonicalizedHeaders + canonicalizedResource;

System.out.println("StringToSign:[" + canonicalString + "]");

String signature = null;

try {

signature = Signature.signWithHmacSha1(securityKey, canonicalString);

// 上传的文件目录

inputStream = new FileInputStream("D:\\OBSobject\\text01.txt");

InputStreamEntity entity = new InputStreamEntity(inputStream);

httpPut.setEntity(entity);

System.out.println(httpResponse.getStatusLine());

for (Header header : httpResponse.getAllHeaders()) {

System.out.println(header.getName() + ":" + header.getValue());

}

BufferedReader reader = new BufferedReader(new InputStreamReader(

httpResponse.getEntity().getContent()));

package com.obsclient;

import java.text.DateFormat;

import java.text.SimpleDateFormat;

import java.util.Locale;

import java.util.TimeZone;

public class DateUtils {

public static String formatDate(long time) {

DateFormat serverDateFormat = new SimpleDateFormat("EEE, dd MMM yyyy HH:mm:ss z", Locale.ENGLISH);

serverDateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));

return serverDateFormat.format(time);

} }

签名字符串Signature的计算方法为:

package com.obsclient;

import javax.crypto.Mac;

import javax.crypto.spec.SecretKeySpec;

import java.io.UnsupportedEncodingException;

import java.security.NoSuchAlgorithmException;

import java.security.InvalidKeyException;

import java.util.Base64;

public class Signature {

public static String signWithHmacSha1(String sk, String canonicalString) throws UnsupportedEncodingException {

try {

SecretKeySpec signingKey = new SecretKeySpec(sk.getBytes("UTF-8"), "HmacSHA1");

Mac mac = Mac.getInstance("HmacSHA1");

mac.init(signingKey);

return Base64.getEncoder().encodeToString(mac.doFinal(canonicalString.getBytes("UTF-8")));

} catch (NoSuchAlgorithmException | InvalidKeyException | UnsupportedEncodingException e) { e.printStackTrace();

}

return null;

} }

5 API

5.1 桶的基础操作 5.2 桶的高级配置 5.3 静态网站托管 5.4 对象操作 5.5 多段操作 5.6 服务端加密 5.7 Data+

5.1 桶的基础操作

5.1.1 获取桶列表

功能介绍

OBS用户可以通过请求查询自己创建的桶列表。

请求消息样式

GET / HTTP/1.1

Host: obs.cn-north-4.myhuaweicloud.com Date: date

Authorization: authorization

请求消息参数

该请求消息中不带请求参数。

请求消息头

该操作消息头与普通请求一样,请参见表3-3,但可以带附加消息头,附加请求消息头 如下所示。

5-1 附加请求消息头

消息头名称 描述 是否必选

x-obs-bucket-type 通过此消息头明确获取的列表内容。

取值:

● OBJECT:获取所有桶列表。

● POSIX:获取所有并行文件系统列表。

不带此消息头则获取所有桶和并行文件系统列 表。

类型:字符串

示例:x-obs-bucket-type: POSIX

请求消息元素

该请求消息中不带请求元素。

响应消息样式

GET HTTP/1.1 status_code Content-Type: type Date: date

Content-Length: length

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ListAllMyBucketsResult xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">

<Owner>

<ID>id</ID>

</Owner>

<Buckets>

<Bucket>

<Name>bucketName</Name>

<CreationDate>date</CreationDate>

<Location>region</Location>

<BucketType>buckettype</BucketType>

</Bucket>

...

</Buckets>

</ListAllMyBucketsResult>

响应消息头

该请求的响应消息使用公共消息头,具体请参考表3-22。

响应消息元素

该请求的响应消息中,会以XML形式将用户拥有的桶列出来,元素的具体含义如表5-2 所示。

5-2 响应消息元素

元素名称 描述

ListAllMyBucketsResult 用户的桶列表。

类型:XML。

Owner 桶拥有者信息,包含租户ID。

类型:XML。

ID 用户的DomainID(帐号ID)。

类型:字符串。

Buckets 用户所拥有的桶列表。

类型:XML。

Bucket 具体的桶信息。

类型:XML。

Name 桶名称。

类型:字符串。

CreationDate 桶的创建时间。

类型:字符串。

Location 桶的位置信息。

类型:字符串

BucketType 桶类型。

类型:字符串。

● OBJECT:对象存储桶。

● POSIX:并行文件系统。

错误响应消息

该请求无特殊错误,所有错误已经包含在表7-3中。

请求示例

GET / HTTP/1.1 User-Agent: curl/7.29.0

Host: obs.cn-north-4.myhuaweicloud.com Accept: */*

Date: Mon, 25 Jun 2018 05:37:12 +0000

Authorization: OBS GKDF4C7Q6SI0IPGTXTJN:9HXkVQIiQKw33UEmyBI4rWrzmic=

响应示例

HTTP/1.1 200 OK Server: OBS

x-obs-request-id: BF260000016435722C11379647A8A00A

x-obs-id-2: 32AAAQAAEAABAAAQAAEAABAAAQAAEAABCSGGDRUM62QZi3hGP8Fz3gOloYCfZ39U Content-Type: application/xml

Date: Mon, 25 Jun 2018 05:37:12 GMT

Content-Length: 460

<?xml version="1.0" encoding="UTF-8" standalone="yes"?>

<ListAllMyBucketsResult xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">

<Owner>

<ID>783fc6652cf246c096ea836694f71855</ID>

</Owner>

<Buckets>

<Bucket>

<Name>examplebucket01</Name>

<CreationDate>2018-06-21T09:15:01.032Z</CreationDate>

<Location>cn-north-4</Location>

<BucketType>OBJECT</BucketType>

</Bucket>

<Bucket>

<Name>examplebucket02</Name>

<CreationDate>2018-06-22T03:56:33.700Z</CreationDate>

<Location>cn-east-3</Location>

<BucketType>OBJECT</BucketType>

</Bucket>

</Buckets>

</ListAllMyBucketsResult>

5.1.2 创建桶

功能介绍

创建桶是指按照用户指定的桶名创建一个新桶的操作。

说明

● 默认情况下,一个用户可以拥有的桶的数量不能超过100个。

● 用户删除桶后,需要等待30分钟才能创建同名桶和并行文件系统。

● OBS支持在创建桶时指定桶的AZ类型,您可以开启或关闭多AZ。关闭多AZ时,桶内数据默 认存储在单个AZ内;开启多AZ时,桶内数据冗余存储在多个AZ内,可靠性更高。旧桶AZ类 型默认为单AZ。

新创建桶的桶名在OBS中必须是唯一的。如果是同一个用户重复创建同一区域的同名 桶时返回成功。除此以外的其他场景重复创建同名桶返回桶已存在。用户可以在请求 消息头中加入x-obs-acl等参数,设置要创建桶的权限控制策略。

存储类型

允许用户创建不同默认存储类型的桶。发送创桶请求时携带头域“x-obs-storage-class”来指定桶的默认存储类型。桶内对象的存储类型与桶默认存储类型保持一致。

存储类型有3种:STANDARD(标准存储)、WARM(低频访问存储)、COLD(归档 存储)。如果没有携带此头域 ,则创建的桶为标准存储类型。

当往桶内上传对象时,如果没有指定对象的存储类别(参考5.4.1 PUT上传),则该对 象的存储类型取桶的默认存储类型。

● OBS标准存储拥有低访问时延和较高的吞吐量,因而适用于有大量热点文件需要 频繁访问数据的业务场景,例如:大数据、移动应用、热点视频、社交图片等场 景。

● OBS低频访问存储适用于不频繁访问(少于每月一次访问)但在需要时也要求快 速访问数据的业务场景,例如:文件同步/共享、企业备份等场景。与标准存储相 比,低频访问存储有相同的数据持久性、吞吐量以及访问时延,且成本较低,但 是可用性略低于标准存储。

● OBS归档存储适用于很少访问(平均一年访问一次)数据的业务场景,例如:数 据归档、长期备份等场景。归档存储安全、持久且成本极低,可以用来替代磁带 库。为了保持成本低廉,数据取回时间可能长达数分钟到数小时不等。

请求消息样式

PUT / HTTP/1.1

Host: bucketname.obs.cn-north-4.myhuaweicloud.com Content-Length: length

Date: date

Authorization: authorization x-obs-az-redundancy:3az

<CreateBucketConfiguration xmlns="http://obs.cn-north-4.myhuaweicloud.com/doc/2015-06-30/">

<Location>location</Location>

</CreateBucketConfiguration>

请求消息参数

该请求消息中不带请求参数。

请求消息头

该操作消息头与普通请求一样,请参见表3-3,但可以带附加消息头,附加请求消息头 如下所示。

5-3 附加请求消息头

消息头名称 描述 是否必选

x-obs-acl 创建桶时,可以加上此消息头设置桶的权限控制 策略,使用的策略为预定义的常用策略,包括:

private、public-read、public-read-write、

public-read-delivered、public-read-write-delivered、bucket-owner-full-control(各策略 详细说明见ACL章节的“使用头域设置

ACL”)。

类型:字符串。

x-obs-storage-class 创建桶时,可以加上此消息头设置桶的默认存储 类型,默认存储类型有3种:STANDARD(标准 存储)、WARM(低频访问存储)、COLD(归

x-obs-storage-class 创建桶时,可以加上此消息头设置桶的默认存储 类型,默认存储类型有3种:STANDARD(标准 存储)、WARM(低频访问存储)、COLD(归