• Object Storage Service

obs
  1. Help Center
  2. Object Storage Service
  3. Developer Guide (Java SDK)
  4. Temporary Authentication
  5. Temporarily Authorized Request

Temporarily Authorized Request

You can call ObsClient.createTemporarySignature to create a URL for a temporarily authorized request. The following sample code creates a URL for common operations, including bucket creation, as well as object upload, download, listing, and deletion.

String endPoint = "yourdomainname";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";

// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// Specify the validity period of the URL to 3600 seconds.
long expireSeconds = 60 * 60;

// Create a bucket.
TemporarySignatureRequest request = new TemporarySignatureRequest(HttpMethodEnum.PUT, expireSeconds);
request.setBucketName("bucketname");
TemporarySignatureResponse response = obsClient.createTemporarySignature(request);
System.out.println("\t" + response.getSignedUrl());

// Upload an object.
Map<String, String> headers = new HashMap<String, String>();
headers.put("Content-Type", "text/plain");
request = new TemporarySignatureRequest(HttpMethodEnum.PUT, expireSeconds);
request.setHeaders(headers);
request.setBucketName("bucketname");
request.setObjectKey("objectkey");
response = obsClient.createTemporarySignature(request);
System.out.println("\t" + response.getSignedUrl());

// Download an object.
request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
request.setBucketName("bucketname");
request.setObjectKey("objectkey");
response = obsClient.createTemporarySignature(request);
System.out.println("\t" + response.getSignedUrl());

// List objects.
request = new TemporarySignatureRequest(HttpMethodEnum.GET, expireSeconds);
request.setBucketName("bucketname");
response = obsClient.createTemporarySignature(request);
System.out.println("\t" + response.getSignedUrl());

// Delete an object.
request = new TemporarySignatureRequest(HttpMethodEnum.DELETE, expireSeconds);
request.setBucketName("bucketname");
request.setObjectKey("objectkey");
response = obsClient.createTemporarySignature(request);
System.out.println("\t" + response.getSignedUrl());

// Close obsClient.
obsClient.close();
NOTE:
  • HttpMethodEnum is an enumeration function defined in OBS Java SDK, whose value indicates the request method types.