OSS会为每一个存储空间(Bucket)分配默认的访问域名,本文介绍OSS访问域名的构成规则及使用方式。
针对OSS的网络请求,除了GetService这个API以外,其他所有请求的域名都是带有指定Bucket信息的域名组成的。
访问域名结构:BucketName.Endpoint。
BucketName为您的存储空间名称,Endpoint为存储空间对应的地域域名。
说明:
外网(公网)指的是通过互联网访问OSS。通过外网访问产生的流入流量(写)是免费的,流出流量(读)是收费的。
说明: OSS费用详情请参见OSS价格总览。
外网访问OSS有如下两种方式:
<Schema>://<Bucket>.<外网Endpoint>/<Object>
示例:如您的Region为华北-北京(s3.cn-north-1.jdcloud-oss.com),Bucket名称为123,Object访问路径为myfile/aaa.txt,那么您的外网访问地址为:
123.s3.cn-north-1.jdcloud-oss.com/myfile/aaa.txt
说明:
OSS访问域名需携带Object访问路径才可以被访问,仅访问域名,如123.s3.cn-north-1.jdcloud-oss.com
,会有报错提示。
您还可以直接将Object的URL放入HTML中使用,如下所示:
<img src="https://123.s3.cn-north-1.jdcloud-oss.com/myfile/aaa.png">
访问方式二: 通过OSS SDK配置外网访问域名。
OSS SDK会对您的每一个操作拼接访问域名。但您在对不同地域的Bucket进行操作的时候需要设置不同的Endpoint。
以Java SDK为例,对位于华北-北京的Bucket进行操作时,需要在对类实例化时设置Endpoint:
public class S3SdkTest{
public static void main(String[ ] args) {
final String accessKey = "<your accesskey>";
final String secretKey = "<your secretkey>";
final String endpoint = "https://s3.cn-north-1.jdcloud-oss.com";
ClientConfiguration config = new ClientConfiguration();
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(endpoint, "cn-north-1");
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey,secretKey);
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
AmazonS3 s3 = AmazonS3Client.builder()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(config)
.withCredentials(awsCredentialsProvider)
.disableChunkedEncoding()
.build();
}
}
内网指的是京东云产品之间的内网通信网络,例如您通过云主机(VM)访问OSS服务。内网产生的流入和流出流量均免费,其他费用照常计费。
内网访问OSS有如下两种方式:
<Schema>://<Bucket>.<内网Endpoint>/<Object>
123.s3-internal.cn-north-1.jdcloud-oss.com/myfile/aaa.txt
访问方式二:通过VM使用OSS SDK配置内网访问域名。
以Java SDK为例,对位于华北-北京的Bucket进行操作时,需要在对类实例化时设置Endpoint:
public class S3SdkTest{
public static void main(String[ ] args) {
final String accessKey = "<your accesskey>";
final String secretKey = "<your secretkey>";
final String endpoint = "https://s3-internal.cn-north-1.jdcloud-oss.com";
ClientConfiguration config = new ClientConfiguration();
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(endpoint, "cn-north-1");
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey,secretKey);
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
AmazonS3 s3 = AmazonS3Client.builder()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(config)
.withCredentials(awsCredentialsProvider)
.disableChunkedEncoding()
.build();
}
}
说明:
同一个Region的VM和OSS之间内网互通,不同Region的VM和OSS之间内网不互通,只能使用外网访问域名。
例如,您的OSS有两个Bucket,并且购买了华北-北京(s3-internal.cn-north-1.jdcloud-oss.com)的VM:
s3-internal.cn-north-1.jdcloud-oss.com
来访问examplebeijing的资源。s3-internal.cn-south-1.jdcloud-oss.com
是无法访问OSS的,必须使用外网地址s3.cn-south-1.jdcloud-oss.com
。IPv6域名在内外网均可使用,在IPv4和IPv6环境下也都可以使用,但需要使用IPv6环境的客户端才能访问IPv6地址。
IPv6访问OSS有如下两种方式:
<Schema>://<Bucket>.<IPv6 Endpoint>/<Object>
123.s3-ipv6.cn-north-1.jdcloud-oss.com/myfile/aaa.txt
访问方式二:通过支持IPv6的VM使用OSS SDK配置IPv6访问域名。
以Java SDK为例,对位于华北-北京的Bucket进行操作时,需要在对类实例化时设置Endpoint:
public class S3SdkTest{
public static void main(String[ ] args) {
final String accessKey = "<your accesskey>";
final String secretKey = "<your secretkey>";
final String endpoint = "https://s3-ipv6.cn-north-1.jdcloud-oss.com";
ClientConfiguration config = new ClientConfiguration();
AwsClientBuilder.EndpointConfiguration endpointConfig =
new AwsClientBuilder.EndpointConfiguration(endpoint, "cn-north-1");
AWSCredentials awsCredentials = new BasicAWSCredentials(accessKey,secretKey);
AWSCredentialsProvider awsCredentialsProvider = new AWSStaticCredentialsProvider(awsCredentials);
AmazonS3 s3 = AmazonS3Client.builder()
.withEndpointConfiguration(endpointConfig)
.withClientConfiguration(config)
.withCredentials(awsCredentialsProvider)
.disableChunkedEncoding()
.build();
}
}