Creating a Signed URL

API Description

You can use this API to create a URL whose Query parameters are carried with authentication information by specifying the AK and SK, HTTP method, and request parameters. You can provide other users with this URL for temporary access. When generating a URL, you need to specify the validity period of the URL to restrict the access duration of visitors.

If you want to grant other users the permission to perform other operations on buckets or objects (for example, upload or download objects), generate a URL with the corresponding request (for example, to upload an object using the URL that generates the PUT request) and provide the URL for other users.

Method Definition

ObsClient.createSignedUrl(method, bucketName, objectKey, specialParam, expires, headers, queryParams)

Request Parameters

Field

Type

Optional or Mandatory

Description

method

str

Mandatory

HTTP method. Possible values are:

  • GET
  • POST
  • PUT
  • DELETE
  • HEAD

bucketName

str

Optional

Bucket name

objectKey

str

Optional

Object name

specialParam

str

Optional

Special operator, which indicates the sub-resource to be operated. Possible values are:

  • versions
  • uploads
  • location
  • storageinfo
  • quota
  • storagePolicy
  • acl
  • append
  • logging
  • policy
  • lifecycle
  • website
  • versioning
  • cors
  • notification
  • tagging
  • delete
  • restore

expires

int

Optional

Expiration time of the signed URL, in seconds. The default value is 300 and the maximum value is 2592000 (30 days).

headers

dict

Optional

Headers in the request

queryParams

dict

Optional

Query parameters in the request

Returned Results

Field

Type

Description

signedUrl

str

URL with authentication information

actualSignedRequestHeaders

dict

Actual headers in the request initiated by using the signed URL

Sample Code

try:
    # Generate a signed URL for creating a bucket.
    res = obsClient.createSignedUrl('PUT', 'bucketname', expires= 3600) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
     
    # Generate a signed URL for uploading an object.
    res = obsClient.createSignedUrl('PUT', 'bucketname', 'objectkey', expires= 3600, headers={'Content-Type' : 'text/plain'}) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
     
    # Generate a signed URL for setting an object ACL.
    res = obsClient.createSignedUrl('PUT', 'bucketname', 'objectkey', 'acl', expires= 3600, headers={'x-obs-acl' : 'public-read'}) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
     
    # Generate a signed URL for downloading an object.
    res = obsClient.createSignedUrl('GET', 'bucketname', 'objectkey', expires= 3600) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
     
    # Generate a signed URL for deleting an object.
    res = obsClient.createSignedUrl('DELETE', 'bucketname', 'objectkey', expires= 3600) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
     
    # Generate a signed URL for deleting a bucket.
    res = obsClient.createSignedUrl('DELETE', 'bucketname', expires= 3600) 
    print('signedUrl:', res.signedUrl) 
    print('actualSignedRequestHeaders:', res.actualSignedRequestHeaders) 
except:
    import traceback
    print(traceback.format_exc())