After you upload objects to a bucket, you can share the URLs of the objects with third parties for downloads or previews.

Use the OSS console

  1. Log on to the OSS console.
  2. In the left-side navigation pane, click Buckets. On the Buckets page, click the name of the desired bucket.
  3. In the left-side navigation pane, choose Files > Files.
  4. Obtain the URLs of objects.
    • Obtain the URL of a single object
      1. Click the name of the object that you want to share.
      2. In the View Details panel, configure the parameters described in the following table. Then, click Copy File URL.
        Parameter Description
        Validity Period If the ACL of the object that you want to share is private, you must set a validity period for the URL of the object.

        Valid values: 60 to 32400

        Unit: seconds

        To obtain a URL that has a longer validity period, we recommend that you use ossutil or ossbrowser.

        Custom Domain Name To ensure that an image object or a web page object is previewed but not downloaded when the object is accessed by third parties, generate the URL of the object by using the custom domain name mapped to the bucket.

        You can configure this parameter only when a custom domain name is mapped to the bucket. For more information, see Map custom domain names.

        HTTPS By default, the URL of an object is generated using HTTPS. To use HTTP to generate a URL for the object, turn off HTTPS.
    • Obtain the URLs of objects at a time
      1. Select the objects that you want to share. Choose Batch Operation > Export URL List.
      2. In the Export URL List panel, configure the parameters described in the following table.
        Parameter Description
        HTTPS By default, the URLs of objects are generated using HTTPS. To use HTTP to generate object URLs, turn off HTTPS.
        Validity Period If the ACL of the objects that you want to share is private, you must set a validity period for the URLs of the objects.

        Valid values: 60 to 32400

        Unit: seconds

        To obtain URLs that have a longer validity period, we recommend that you use ossutil or ossbrowser.

        Custom Domain Name To ensure that image objects or web page objects are previewed but not downloaded when the objects are accessed by third parties, generate the URLs of the objects by using the custom domain name mapped to the bucket.

        You can configure this parameter only when a custom domain name is mapped to the bucket. For more information, see Map custom domain names.

        Accelerate Endpoint If third parties located far from your data centers need to access the shared objects, we recommend that you use the acceleration endpoint of the bucket to generate the URLs of the objects.

        You can configure this parameter only when transfer acceleration is enabled for the bucket. For more information, see Enable transfer acceleration.

      3. Click OK and then export the URL list as a local file.
  5. Share the URL list file with third parties for previews or downloads.

Use OSS SDKs

The following code provides examples on how to use OSS SDKs for common programming languages to share objects with third parties for previews and downloads by providing the object URLs. For more information about how to use OSS SDKs for other programming languages to share objects with third parties for previews and downloads by providing the object URLs, see the OSS SDK overview.

import com.aliyun.oss.*;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.OSSObject;
import java.net.URL;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

public class Demo {

    public static void main(String[] args) throws Exception {
        // In this example, the endpoint of the China (Hangzhou) region is used. Specify your actual endpoint. 
        String endpoint = "https://oss-cn-hangzhou.aliyuncs.com";
        // Specify the temporary AccessKey pair obtained from STS. 
        String accessKeyId = "yourAccessKeyId";
        String accessKeySecret = "yourAccessKeySecret";
        // Specify the security token obtained from STS. 
        String securityToken = "yourSecurityToken";
        // Specify the name of the bucket. Example: examplebucket. 
        String bucketName = "examplebucket";
        // Specify the full path of the object. Example: exampleobject.txt. The path cannot contain the bucket name. 
        String objectName = "exampleobject.txt";        

        // Create an OSSClient instance. 
        OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
        // If you use STS, use the following code to initialize the instance. 
        // After you obtain the temporary access credentials from STS, you can use the security token and temporary AccessKey pair that are contained in the credentials to create an OSSClient instance. 
        // OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret, securityToken);
        try {
            // Specify the validity period of the signed URL. Unit: milliseconds. 
            Date expiration = new Date(new Date().getTime() + 3600 * 1000);

            // Generate the signed URL. 
            GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.GET);
            // Specify the expiration time of the signed URL. 
            request.setExpiration(expiration);
            Map<String, String> headers = new HashMap<String, String>();
            // To automatically download the object when the object is accessed by using a browser and specify the name of the downloaded object, set the Content-Disposition header in the configuration file to attachment. 
            headers.put("content-disposition","attachment");
            // To preview the object when you use the signed URL to access the object in a browser, set the Content-Disposition header to inline and use the custom domain name that is mapped to the bucket to access the object. 
            // headers.put("content-disposition","inline");
            request.setHeaders(headers);
            // Generate the signed URL that allows HTTP GET requests. 
            URL signedUrl = ossClient.generatePresignedUrl(request);
            System.out.println("signed url for getObject: " + signedUrl);

            Map<String, String> customHeaders = new HashMap<String, String>();
            // Add headers to a GetObject request. 
            customHeaders.put("Range", "bytes=100-1000");
            // Use the signed URL to send a request. 
            OSSObject object = ossClient.getObject(signedUrl, customHeaders);
        } 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();
            }
        }
    }
}
<?php
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;

// Specify the temporary AccessKey pair obtained from STS. The AccessKey pair consists of an AccessKey ID and an AccessKey secret. 
$accessKeyId = "yourAccessKeyId";
$accessKeySecret = "yourAccessKeySecret";
// Specify the security token obtained from STS. 
$securityToken = "yourSecurityToken";
// Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
$endpoint = "yourEndpoint";
// Specify the name of the bucket. 
$bucket= "examplebucket";
// Specify the full path of the object. The full path of the object cannot contain the bucket name. 
$object = "exampleobject.txt";
// Set the validity period of the signed URL to 3600. Unit: seconds. 
$timeout = 3600;
A signed URL is generated to preview an object, and the custom domain name mapped to the bucket in which the object is stored is used for access. 
$options= array(
    "response-content-disposition"=>"inline",);
// Generate a signed URL to download an object. 
/*$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");

// You can develop code to access the signed URL, or enter the URL in the address box of a browser to access the object. 
$request = new RequestCore($signedUrl);
// Set the default method to access the signed URL to GET. 
$request->set_method('GET');
$request->add_header('Content-Type', '');
$request->send_request();
$res = new ResponseCore($request->get_response_header(), $request->get_response_body(), $request->get_response_code());
if ($res->isOK()) {
    print(__FUNCTION__ . ": OK" . "\n");
} else {
    print(__FUNCTION__ . ": FAILED" . "\n");
};                  
const OSS = require('ali-oss');

const client = new OSS({
  // Specify the region in which the bucket is located. For example, if your bucket is located in the China (Hangzhou) region, set the region to oss-cn-hangzhou. 
  region: 'yourRegion',
  // The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a RAM user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
  accessKeyId: 'yourAccessKeyId',
  accessKeySecret: 'yourAccessKeySecret',
  // Specify the name of the bucket. 
  bucket: 'examplebucket'
});

// Obtain the signed URL used to download the exampleobject.txt object. By default, if you use the signed URL in a browser to access the object, the object is previewed but not downloaded. 
// Specify the full path of the object. The path cannot contain the bucket name. 
const url = client.signatureUrl('exampleobject.txt');
console.log(url);

// Obtain the signed URL used to download the exampleobject.txt object, and set the Content-Disposition header to attachment. This way, if you use the signed URL to access the object in a browser, the object is automatically downloaded, and you can specify the name of the downloaded object. 
// To preview the object when you use the signed URL to access the object in a browser, set the Content-Disposition header to inline and use the custom domain name that is mapped to the bucket to access the object. 
const filename = 'ossdemo.txt' // Specify the name of the downloaded object. 
const response = {
  'content-disposition': `attachment; filename=${encodeURIComponent(filename)}`
}
// Specify the full path of the object. The path cannot contain the bucket name. 
const url = client.signatureUrl('exampleobject.txt', { response });
console.log(url);

// Obtain the signed URL used to upload the exampleobject.txt object and set the validity period of the URL. 
// Specify the full path of the object. The path cannot contain the bucket name. 
const url = client.signatureUrl('exampleobject.txt', {
  // Specify the validity period of the signed URL. By default, the validity period is 1,800 seconds. 
  expires: 3600, 
  // Set the request method to PUT. By default, the request method is GET. 
  method: 'PUT'  
});
console.log(url);

// Obtain the signed URL used to upload the exampleobject.txt object and specify the Content-Type parameter. 
// Specify the full path of the object. The path cannot contain the bucket name. 
const url = client.signatureUrl('exampleobject.txt', {
  expires: 3600, 
  method: 'PUT',
  'Content-Type': 'text/plain; charset=UTF-8',
});
console.log(url);
# -*- coding: utf-8 -*-
import oss2
# The AccessKey pair of an Alibaba Cloud account has permissions on all API operations. Using these credentials to perform operations in OSS is a high-risk operation. We recommend that you use a Resource Access Management (RAM) user to call API operations or perform routine O&M. To create a RAM user, log on to the RAM console. 
auth = oss2.Auth('yourAccessKeyId', 'yourAccessKeySecret')
# Specify the endpoint of the region in which the bucket is located. For example, if the bucket is located in the China (Hangzhou) region, set the endpoint to https://oss-cn-hangzhou.aliyuncs.com. 
# Specify the name of the bucket. Example: examplebucket. 
bucket = oss2.Bucket(auth, 'yourEndpoint', 'examplebucket')
# Specify the full path of the object. Example: exampledir/exampleobject.txt. The full path cannot contain the bucket name. 
object_name = 'exampledir/exampleobject.txt'

# Specify a header. 
headers = dict()
# To implement automatic download when the object is accessed by using a browser and specify the name of the downloaded object, set the Content-Disposition header in the configuration file to attachment.
# headers['content-disposition'] = 'attachment'
# To preview the object when you use the signed URL to access the object in a browser, set the Content-Disposition header to inline and use the custom domain name that is mapped to the bucket to access the object. 
headers['content-disposition'] = 'inline'

# Generate the signed URL that is used to download the object. The validity period of the URL is 60 seconds. 
# By default, OSS identifies the forward slashes (/) in the full path of an object as escape characters when the signed URL is generated. Therefore, you cannot directly use the signed URL. 
# Set the slash_safe parameter to True. This way, OSS does not identify the forward slashes (/) in the full path of the object as escape characters. Then, you can directly use the generated signed URL. 
url = bucket.sign_url('GET', object_name, 60, headers=headers, slash_safe=True)
print('the address of the signed URL:', url)

# Use the signed URL to download the object to a local path. 
# Specify the full path of the local file to upload. Example: D:\\localpath\\examplefile.txt. 
# By default, if you set this parameter to the name of a local file such as examplefile.txt without specifying the local path, the downloaded object is saved to the local path of the project to which the sample program belongs. 
result = bucket.get_object_with_url_to_file(url, 'D:\\localpath\\examplefile.txt')
print(result.read())