获取单个文件的URL
公共读Object
如果文件的读写权限ACL为公共读,即该文件允许匿名访问,则文件URL的格式为
https://BucketName.Endpoint/ObjectName
。其中,ObjectName需填写包含文件夹以及文件后缀在内的该文件的完整路径。各地域的Endpoint信息介绍,请参见
访问域名和数据中心
。
例如华东1(杭州)地域下名为examplebucket的Bucket下有名为example的文件夹,文件夹内有个名为example.jpg的文件。则该文件URL为:
外网访问URL:
https://examplebucket.oss-cn-hangzhou.aliyuncs.com/example/example.jpg
内网访问URL(供同地域ECS实例访问):
https://examplebucket.oss-cn-hangzhou-internal.aliyuncs.com/example/example.jpg
如果文件所在的Bucket绑定了自定义域名,则文件URL的格式为
https://YourDomainName/ObjectName
。
例如您在华东1(杭州)地域下的examplebucket绑定了自定义域名
example.com
,且该Bucket下包含
example.jpg
的文件,则该文件URL为
https://example.com/example.jpg
。
如需确保通过文件URL访问文件时是预览行为,您需要绑定自定义域名并添加CNAME记录。详情请参见
使用自有域名访问OSS资源
。
私有Object
如果文件读写权限ACL为私有,则必须进行签名操作。私有文件URL的格式为
https://BucketName.Endpoint/Object?签名参数
。您可以通过以下任意方法获取文件URL并设置URL的有效时长。
使用OSS控制台
您可以通过OSS控制台获取单个文件的URL。文件URL的有效时长因账号类型存在差异。例如,阿里云账号可设置的文件URL有效时长最大为32400秒(9小时),RAM用户以及STS用户可设置的文件URL有效时长最大为3600秒(1小时)。如需获取更长时效的文件URL,请使用命令行工具ossutil、图形化管理工具ossbrowser或阿里云SDK。
登录
OSS管理控制台
。
单击
Bucket列表
,然后单击目标Bucket名称。
在左侧导航栏,选择
。
获取文件URL。
获取单个文件URL
单击目标文件名称。
在
详情
面板配置以下参数,然后单击
复制文件URL
。
当目标文件为私有文件时,需设置文件URL的有效时间。
取值范围:60~32400
单位:秒
如果您希望设置更长过期时间的文件URL,您可以使用ossbrowser、sdk、ossutil等工具。
如需确保第三方访问图片或网页文件时是预览行为,请使用Bucket绑定的自定义域名生成文件URL。
仅当Bucket绑定自定义域名后可配置此项。更多信息,请参见
绑定自定义域名
。
使用HTTPS
默认使用HTTPS协议生成文件URL。如需使用HTTP协议生成文件URL,请关闭
使用HTTPS
开关。
当目标文件为私有文件时,需设置文件URL的有效时间。
取值范围:60~32400
单位:秒
若您希望获取更长时效的文件URL,建议使用
命令行工具ossutil
或
图形化工具ossbrowser
。
如需确保第三方访问图片或网页文件时是预览行为,请使用Bucket绑定的自定义域名生成文件URL。
仅当Bucket绑定自定义域名后可配置此项。更多信息,请参见
绑定自定义域名
。
传输加速域名
若第三方涉及跨国或跨洋等超远距离文件访问场景时,建议使用传输加速域名生成文件URL。
仅当Bucket开启传输加速后可配置此项。更多信息,请参见
开启传输加速
。
单击
确定
,然后将URL列表文件保存到本地。
使用图形化管理工具ossbrowser
ossbrowser支持Object级别的操作与控制台支持的操作类似,请按照ossbrowser界面指引完成获取签名URL的操作。关于如何使用ossbrowser,请参见
快速使用ossbrowser
。
使用阿里云SDK
以下仅列举常见SDK的生成签名URL代码示例。关于其他SDK的生成签名URL代码示例,请参见
SDK简介
。
说明
通过STS服务生成签名URL时,需要填写STS服务获取的临时访问凭证。临时访问凭证包括临时访问密钥(AccessKey ID和AccessKey Secret)以及安全令牌(SecurityToken)。关于如何获取临时访问凭证的具体操作,请参见
使用STS临时访问凭证访问OSS
。
public class Demo {
public static void main(String[] args) throws Throwable {
// 以华东1(杭州)的外网Endpoint为例,其它Region请按实际情况填写。
String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
String accessKeyId = "yourAccessKeyId";
String accessKeySecret = "yourAccessKeySecret";
// 从STS服务获取的安全令牌(SecurityToken)。
String securityToken = "yourSecurityToken";
// 填写Bucket名称,例如examplebucket。
String bucketName = "examplebucket";
// 填写Object完整路径,例如exampleobject.txt。Object完整路径中不能包含Bucket名称。
String objectName = "exampleobject.txt";
// 从STS服务获取临时访问凭证后,您可以通过临时访问密钥和安全令牌生成OSSClient。
// 创建OSSClient实例。
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);
try {
// 设置签名URL过期时间,单位为毫秒。
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 生成以GET方法访问的签名URL,访客可以直接通过浏览器访问相关内容。
URL url = ossClient.generatePresignedUrl(bucketName, objectName, expiration);
System.out.println(url);
} catch (OSSException oe) {
System.out.println("Caught an OSSException, which means your request made it to OSS, "
+ "but was rejected with an error response for some reason.");
System.out.println("Error Message:" + oe.getErrorMessage());
System.out.println("Error Code:" + oe.getErrorCode());
System.out.println("Request ID:" + oe.getRequestId());
System.out.println("Host ID:" + oe.getHostId());
} catch (ClientException ce) {
System.out.println("Caught an ClientException, which means the client encountered "
+ "a serious internal problem while trying to communicate with OSS, "
+ "such as not being able to access the network.");
System.out.println("Error Message:" + ce.getMessage());
} finally {
if (ossClient != null) {
ossClient.shutdown();
if (is_file(__DIR__ . '/../autoload.php')) {
require_once __DIR__ . '/../autoload.php';
if (is_file(__DIR__ . '/../vendor/autoload.php')) {
require_once __DIR__ . '/../vendor/autoload.php';
use OSS\OssClient;
use OSS\Core\OssException;
use OSS\Http\RequestCore;
use OSS\Http\ResponseCore;
// 从STS服务获取的临时访问密钥(AccessKey ID和AccessKey Secret)。
$accessKeyId = "
yourAccessKeyId
";
$accessKeySecret = "
yourAccessKeySecret
";
// 从STS服务获取的安全令牌(SecurityToken)。
$securityToken = "
yourSecurityToken
";
//
yourEndpoint
填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
$endpoint = "
yourEndpoint
";
// 填写Bucket名称。
$bucket= "examplebucket";
// 填写不包含Bucket名称在内的Object完整路径。
$object = "exampleobject.txt";
// 设置签名URL的有效时长为3600秒。
$timeout = 3600;
// 生成预览的签名URL,然后使用Bucket绑定的自定义域名进行访问。
$options= array(
"response-content-disposition"=>"inline",);
// 生成下载的签名URL。
/*$options = array(
"response-content-disposition"=>"attachment",
try {
$ossClient = new OssClient($accessKeyId, $accessKeySecret, $endpoint, false, $securityToken);
$signedUrl = $ossClient->signUrl($bucket, $object, $timeout,'GET',$options);
} catch (OssException $e) {
printf(__FUNCTION__ . ": FAILED\n");
printf($e->getMessage() . "\n");
return;
print(__FUNCTION__ . ": signedUrl: " . $signedUrl . "\n");
const { default: axios } = require("axios");
const fs = require("fs");
const client = new OSS({
// yourRegion填写Bucket所在地域。以华东1(杭州)为例,yourRegion填写为oss-cn-hangzhou。
region: 'yourRegion',
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
accessKeyId: 'yourAccessKeyId',
accessKeySecret: 'yourAccessKeySecret',
// 填写Bucket名称。
bucket: 'examplebucket'
// 生成文件的签名URL。
const url = client.signatureUrl("examplefile.txt");
// 通过签名URL下载文件到本地,并填写下载到本地的文件完整路径。
const file = "D:\\localpath\\examplefile.txt";
axios({
method: "GET",
.then((r) => {
fs.writeFile(file, r.data, (err) => {
if (err) {
console.log(err);
.catch((e) => console.log(e));
using Aliyun.OSS;
using Aliyun.OSS.Common;
// 填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。
var endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
// 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。
var accessKeyId = "yourAccessKeyId";
var accessKeySecret = "yourAccessKeySecret";
// 填写Bucket名称,例如examplebucket。
var bucketName = "examplebucket";
// 填写Object完整路径,完整路径中不包含Bucket名称,例如exampledir/exampleobject.txt。
var objectName = "exampledir/exampleobject.txt";
// 填写Object下载到本地文件的完整路径。例如D:\\localpath\\examplefile.txt。如果指定的本地文件存在会覆盖,不存在则新建。
var downloadFilename = "D:\\localpath\\examplefile.txt";
// 创建OSSClient实例。
var client = new OssClient(endpoint, accessKeyId, accessKeySecret);
var metadata = client.GetObjectMetadata(bucketName, objectName);
var etag = metadata.ETag;
// 生成签名URL。
var req = new GeneratePresignedUriRequest(bucketName, objectName, SignHttpMethod.Get)
// 设置签名URL过期时间,默认值为3600秒。
Expiration = DateTime.Now.AddHours(1),
var uri = client.GeneratePresignedUri(req);
catch (OssException ex)
Console.WriteLine("Failed with error code: {0}; Error info: {1}. \nRequestID:{2}\tHostID:{3}",
ex.ErrorCode, ex.Message, ex.RequestId, ex.HostId);
catch (Exception ex)
Console.WriteLine("Failed with error info: {0}", ex.Message);
package main
import (
"fmt"
"github.com/aliyun/aliyun-oss-go-sdk/oss"
func HandleError(err error) {
fmt.Println("Error:", err)
os.Exit(-1)
func main() {
// 获取STS临时凭证后,您可以通过其中的安全令牌(SecurityToken)和临时访问密钥(AccessKeyId和AccessKeySecret)生成OSSClient。
client, err := oss.New("yourEndpoint", "yourAccessKeyId", "yourAccessKeySecret", oss.SecurityToken("yourSecurityToken"))
if err != nil {
HandleError(err)
// 填写Bucket名称,例如examplebucket。
bucketName := "examplebucket"
// 填写文件完整路径,例如exampledir/exampleobject.txt。文件完整路径中不能包含Bucket名称。
objectName := "exampledir/exampleobject.txt"
// 将Object下载到本地文件,并保存到指定的本地路径中。如果指定的本地文件存在会覆盖,不存在则新建。
bucket, err := client.Bucket(bucketName)
if err != nil {
HandleError(err)
// 生成用于下载的签名URL,并指定签名URL的有效时间为60秒。
signedURL, err := bucket.SignURL(objectName, oss.HTTPGet, 60)
if err != nil {
HandleError(err)
fmt.Printf("Sign Url:%s\n", signedURL)
#include <alibabacloud/oss/OssClient.h>
using namespace AlibabaCloud::OSS;
int main(void)
/* 初始化OSS账号信息。*/
/* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
std::string AccessKeyId = "yourAccessKeyId";
std::string AccessKeySecret = "yourAccessKeySecret";
/* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
std::string Endpoint = "yourEndpoint";
/* 填写Bucket名称,例如examplebucket。*/
std::string BucketName = "examplebucket";
/* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
std::string GetobjectUrlName = "exampledir/exampleobject.txt";
/* 初始化网络等资源。*/
InitializeSdk();
ClientConfiguration conf;
OssClient client(Endpoint, AccessKeyId, AccessKeySecret, conf);
/* 设置签名URL有效时长。*/
std::time_t t = std::time(nullptr) + 1200;
/* 生成签名URL。*/
auto genOutcome = client.GeneratePresignedUrl(BucketName, GetobjectUrlName, t, Http::Get);
if (genOutcome.isSuccess()) {
std::cout << "GeneratePresignedUrl success, Gen url:" << genOutcome.result().c_str() << std::endl;
else {
/* 异常处理。*/
std::cout << "GeneratePresignedUrl fail" <<
",code:" << genOutcome.error().Code() <<
",message:" << genOutcome.error().Message() <<
",requestId:" << genOutcome.error().RequestId() << std::endl;
ShutdownSdk();
return -1;
/* 释放网络等资源。*/
ShutdownSdk();
return 0;
#include "oss_api.h"
#include "aos_http_io.h"
/* yourEndpoint填写Bucket所在地域对应的Endpoint。以华东1(杭州)为例,Endpoint填写为https://oss-cn-hangzhou.aliyuncs.com。*/
const char *endpoint = "yourEndpoint";
/* 阿里云账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM用户进行API访问或日常运维,请登录RAM控制台创建RAM用户。*/
const char *access_key_id = "yourAccessKeyId";
const char *access_key_secret = "yourAccessKeySecret";
/* 填写Bucket名称,例如examplebucket。*/
const char *bucket_name = "examplebucket";
/* 填写Object完整路径,完整路径中不能包含Bucket名称,例如exampledir/exampleobject.txt。*/
const char *object_name = "exampledir/exampleobject.txt";
/* 填写本地文件的完整路径。*/
const char *local_filename = "yourLocalFilename";
void init_options(oss_request_options_t *options)
options->config = oss_config_create(options->pool);
/* 用char*类型的字符串初始化aos_string_t类型。*/
aos_str_set(&options->config->endpoint, endpoint);
aos_str_set(&options->config->access_key_id, access_key_id);
aos_str_set(&options->config->access_key_secret, access_key_secret);
/* 是否使用CNAME访问OSS服务。0表示不使用。*/
options->config->is_cname = 0;
/* 设置网络相关参数,例如超时时间等。*/
options->ctl = aos_http_controller_create(options->pool, 0);
int main(int argc, char *argv[])
/* 在程序入口调用aos_http_io_initialize方法来初始化网络、内存等全局资源。*/
if (aos_http_io_initialize(NULL, 0) != AOSE_OK) {
exit(1);
/* 用于内存管理的内存池(pool),等价于apr_pool_t。其实现代码在apr库中。*/
aos_pool_t *pool;
/* 重新创建一个内存池,第二个参数是NULL,表示没有继承其它内存池。*/
aos_pool_create(&pool, NULL);
/* 创建并初始化options,该参数包括endpoint、access_key_id、acces_key_secret、is_cname、curl等全局配置信息。*/
oss_request_options_t *oss_client_options;
/* 在内存池中分配内存给options。*/
oss_client_options = oss_request_options_create(pool);
/* 初始化Client的选项oss_client_options。*/
init_options(oss_client_options);
/* 初始化参数。*/
aos_string_t bucket;
aos_string_t object;
aos_string_t file;
aos_table_t *headers = NULL;
aos_table_t *params = NULL;
aos_table_t *resp_headers = NULL;
aos_status_t *resp_status = NULL;
aos_http_request_t *req;
apr_time_t now;
char *url_str;
aos_string_t url;
int64_t expire_time;
int one_hour = 3600;
aos_str_set(&bucket, bucket_name);
aos_str_set(&object, object_name);
aos_str_set(&file, local_filename);
expire_time = now / 1000000 + one_hour;
headers = aos_table_make(pool, 0);
params = aos_table_make(pool, 0);
req = aos_http_request_create(pool);
req->method = HTTP_GET;
now = apr_time_now(); /* 单位:微秒 */
expire_time = now / 1000000 + one_hour;
/* 生成签名URL。*/
url_str = oss_gen_signed_url(oss_client_options, &bucket, &object, expire_time, req);
aos_str_set(&url, url_str);
/* 释放内存池,相当于释放了请求过程中各资源分配的内存。*/
aos_pool_destroy(pool);
/* 释放之前分配的全局资源。*/
aos_http_io_deinitialize();
return 0;