Help Center> Object Storage Service> SDK Reference> Java> FAQs> How Do I Obtain an Object URL?

How Do I Obtain an Object URL?

If the uploaded object is set to be read by anonymous users, anonymous users can download the object through the object URL directly. Methods to obtain the object URL are as follows:

Method 1: Query by calling the API. After an object is uploaded using the ObsClient API, PutObjectResult is returned. You can call getObjectUrl to obtain the URL of the uploaded object. The sample code is as follows:

String endPoint = "https://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
ObsClient obsClient = new ObsClient(ak, sk, endPoint);
// Call putObject to upload the object and obtain the return result.
PutObjectResult result = obsClient.putObject("bucketname", "objectname", new File("localfile"));
// Read the URL of the uploaded object.
System.out.println("\t" + result.getObjectUrl());

Method 2: Assemble the URL in the format of https://Bucket name.Domain name/Directory level/Object name.

  • If the object resides in the root directory of a bucket, its URL does not contain a directory level.
  • You can click here to view the domain names of each region.
  • For example, if you want to access the object named test.txt in the test folder of bucket testbucket residing in region CN North-Beijing4, the URL of the object is https://testbucket.obs.cn-north-4.myhuaweicloud.com/test/test.txt.