Downloading an Object - Streaming (SDK for Java)
Function
When you call ObsClient.getObject, an instance of ObsObject will be returned. This instance contains the contents of the object such as its name, attributes, input stream, and which bucket it is stored in. You can perform operations on the object input stream to read the contents to a local file or memory.
Restrictions
- To download an object, you must be the bucket owner or have the required permission (obs:object:GetObject in IAM or GetObject in a bucket policy). For details, see Introduction to OBS Access Control, IAM Custom Policies, and Configuring an Object Policy.
- Objects in the Archive storage class can be downloaded only when they are restored.
- Object input streams obtained by ObsObject.getObjectContent must be closed explicitly. Otherwise, resource leakage occurs.
Method
obsClient.getObject(GetObjectRequest request)
Request Parameters
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
request |
Yes |
Explanation: Request parameters for downloading an object. For details, see Table 2. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
bucketName |
String |
Yes |
Explanation: Bucket name. Restrictions:
Default value: None |
objectKey |
String |
Yes |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.eu-west-101.myhuaweicloud.eu/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
versionId |
String |
No |
Explanation: Object version ID. Value range: The value must contain 32 characters. Default value: None |
rangeStart |
Long |
No |
Explanation: Start position for object download. Value range: A non-negative integer. Default value: None |
rangeEnd |
Long |
No |
Explanation: End position for object download. Restrictions: The upper limit of the value is the object length minus 1, in bytes. Default value: None |
ifMatchTag |
String |
No |
Explanation: Preset ETag. If the ETag of the object to be downloaded is the same as the preset ETag, the object is returned. Otherwise, an error is returned. Value range: The value must contain 32 characters. Default value: None |
ifNoneMatchTag |
String |
No |
Explanation: Preset ETag. If the ETag of the object to be downloaded is different from the preset ETag, the object is returned. Otherwise, an error is returned. Value range: The value must contain 32 characters. Default value: None |
IfModifiedSince |
Date |
No |
Explanation: The object is returned if it has been modified since the specified time; otherwise, an error is returned. Default value: None |
IfUnmodifiedSince |
Date |
No |
Explanation: The object is returned if it has not been modified since the specified time; otherwise, an error is returned. Default value: None |
sseCHeader |
No |
Explanation: Server-side decryption header. For details, see Table 7. |
|
replaceMetadata |
No |
Explanation: Additional information about the object. For details, see Table 3. |
|
progressListener |
No |
Explanation: Data transmission listener for obtaining the download progress. For details, see Table 4. |
|
encodeHeaders |
boolean |
No |
Explanation: Whether to enable OBS to automatically encode request headers. Due to HTTP coding restrictions, only ASCII characters can be sent. If your request headers contain full-width characters, the SDK will URL encode these characters before sending the request. When you use a browser to access the object metadata, the browser automatically decodes the data. Value range: true: Encoding with SDK is enabled. false: Encoding with SDK is disabled. Default value: true |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
contentType |
String |
No |
Explanation: MIME type of the object file. MIME type is a standard way of describing a data type and is used by the browser to decide how to display data. Value range: See What Is Content-Type (MIME)? (Java SDK) Default value: None |
contentLanguage |
String |
No |
Explanation: Language or language combination for visitors to customize and use. For details, see the definition of ContentLanguage in the HTTP protocol. Default value: None |
expires |
String |
No |
Explanation: Expires header in the response. It specifies the cache expiration time of the web page when the object is downloaded. Default value: None |
cacheControl |
String |
No |
Explanation: Cache-Control header in the response. It specifies the cache behavior of the web page when an object is downloaded. Default value: None |
contentDisposition |
String |
No |
Explanation: Provides a default file name for the requested object. When the object with the default file name is being downloaded or accessed, the content is displayed as part of a web page in the browser or as an attachment in a download dialog box. Default value: None |
contentEncoding |
String |
No |
Explanation: Content-Encoding header in the response. It specifies which encoding is applied to the object in a download. Default value: None |
Method |
Return Value Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
void |
Yes |
Explanation: Used for obtaining the progress. For details, see Table 5. Default value: None |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
status |
Yes |
Explanation: Progress data. For details, see Table 6. Default value: None |
Method |
Return Value Type |
Description |
---|---|---|
getAverageSpeed() |
double |
Average transmission rate. |
getInstantaneousSpeed() |
double |
Instantaneous transmission rate. |
getTransferPercentage() |
int |
Transmission progress, in percentage. |
getNewlyTransferredBytes() |
long |
Number of the newly transmitted bytes. |
getTransferredBytes() |
long |
Number of bytes that have been transmitted. |
getTotalBytes() |
long |
Number of the bytes to be transmitted. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
algorithm |
Yes |
Explanation: SSE-C is used for encrypting objects on the server side. Value range: AES256, indicating Advanced Encryption Standard (AES) is used to encrypt the object in SSE-C. For details, see Table 9. Default value: None |
|
sseAlgorithm |
No |
Explanation: Encryption algorithm. Restrictions: Only AES256 is supported. Value range: See Table 8. Default value: None |
|
sseCKey |
byte[] |
Yes |
Explanation: Key used for encrypting the object when SSE-C is used, in byte[] format. Default value: None |
sseCKeyBase64 |
String |
No |
Explanation: Base64-encoded key used for encrypting the object when SSE-C is used. Default value: None |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
id |
String |
Yes |
Explanation: Account (domain) ID of the bucket owner. Value range: To obtain the account ID, see How Do I Get My Account ID and User ID? Default value: None |
displayName |
String |
No |
Explanation: Account name of the owner. Value range: To obtain the account name, see How Do I Get My Account ID and User ID? Default value: None |
Constant |
Default Value |
Description |
---|---|---|
STANDARD |
STANDARD |
Standard storage class |
WARM |
WARM |
Infrequent Access storage class. |
COLD |
COLD |
Archive storage class. |
Parameter |
Type |
Mandatory (Yes/No) |
Description |
---|---|---|---|
contentLength |
Long |
No |
Explanation: Object size. Default value: If this parameter is not specified, the SDK automatically calculates the size of the object. |
contentType |
String |
No |
Explanation: MIME type of the object file. MIME type is a standard way of describing a data type and is used by the browser to decide how to display data. Value range: See What Is Content-Type (MIME)? (Java SDK) Default value: If this parameter is not specified, the SDK determines the file type based on the suffix of the object name and assigns a value to the parameter. For example, if the suffix of the object name is .xml, the object is an application/xml file. If the suffix is .html, the object is a text/html file. |
contentEncoding |
String |
No |
Explanation: Content-Encoding header in the response. It specifies which encoding is applied to the object. Default value: None |
contentDisposition |
String |
No |
Explanation: Provides a default file name for the requested object. When the object with the default file name is being downloaded or accessed, the content is displayed as part of a web page in the browser or as an attachment in a download dialog box. Default value: None |
cacheControl |
String |
No |
Explanation: Cache-Control header in the response. It specifies the cache behavior of the web page when an object is downloaded. Default value: None |
contentLanguage |
String |
No |
Explanation: Language or language combination for visitors to customize and use. For details, see the definition of ContentLanguage in the HTTP protocol. Default value: None |
expires |
String |
No |
Explanation: The time a cached web page object expires. Restrictions: The time must be in the GMT format. Default value: None |
contentMd5 |
String |
No |
Explanation: Base64-encoded MD5 value of the object data. It is provided for the OBS server to verify data integrity. The OBS server will compare this MD5 value with the MD5 value calculated based on the object data. If the two values are not the same, HTTP status code 400 is returned. Restrictions:
Value range: Base64-encoded 128-bit MD5 value of the request body calculated according to RFC 1864. Example: n58IG6hfM7vqI4K0vnWpog== Default value: None |
storageClass |
No |
Explanation: Storage class of an object that can be specified at object creation. If you do not specify this header, the object inherits the storage class of the bucket. Value range: See Table 11. Default value: None |
|
webSiteRedirectLocation |
String |
No |
Explanation: If the bucket is configured with website hosting, the request for obtaining the object can be redirected to another object in the bucket or an external URL. This parameter specifies the address the request for the object is redirected to. The request is redirected to an object anotherPage.html in the same bucket: WebsiteRedirectLocation:/anotherPage.html The request is redirected to an external URL http://www.example.com/: WebsiteRedirectLocation:http://www.example.com/ Restrictions:
Default value: None |
nextPosition |
long |
No |
Explanation: Start position for the next append upload. Value range: 0 to the object length, in bytes. Default value: None |
appendable |
boolean |
No |
Explanation: Whether the object is appendable. Value range: true: The object is appendable. false: The object is not appendable. Default value: None |
userMetadata |
Map<String, Object> |
No |
Explanation: User-defined metadata of the object. To define it, you can add a header starting with x-obs-meta- in the request. In Map, the String key indicates the name of the user-defined metadata that starts with x-obs-meta-, and the Object value indicates the value of the user-defined metadata. To obtain the user-defined metadata of an object, use ObsClient.getObjectMetadata. For details, see Obtaining Object Metadata (SDK for Java). Restrictions:
Default value: None |
Responses
Parameter |
Type |
Description |
---|---|---|
bucketName |
String |
Explanation: Bucket name. Restrictions:
Default value: None |
objectKey |
String |
Explanation: Object name. An object is uniquely identified by an object name in a bucket. An object name is a complete path that does not contain the bucket name. For example, if the address for accessing the object is examplebucket.obs.eu-west-101.myhuaweicloud.eu/folder/test.txt, the object name is folder/test.txt. Value range: The value must contain 1 to 1,024 characters. Default value: None |
owner |
Explanation: Owner of the object. For details, see Table 10. |
|
metadata |
Explanation: Object metadata. For details, see Table 4. |
|
objectContent |
InputStream |
Explanation: Object data stream. Default value: None |
Code Examples
This example downloads objectname from examplebucket using streaming.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 |
import com.obs.services.ObsClient; import com.obs.services.exception.ObsException; import com.obs.services.model.ObsObject; import java.io.ByteArrayOutputStream; import java.io.InputStream; public class GetObject001 { public static void main(String[] args) { // Obtain an AK/SK pair using environment variables or import the AK/SK pair in other ways. Using hard coding may result in leakage. // Obtain an AK/SK pair on the management console. String ak = System.getenv("ACCESS_KEY_ID"); String sk = System.getenv("SECRET_ACCESS_KEY_ID"); // (Optional) If you are using a temporary AK/SK pair and a security token to access OBS, you are advised not to use hard coding, which may result in information leakage. // Obtain an AK/SK pair and a security token using environment variables or import them in other ways. // String securityToken = System.getenv("SECURITY_TOKEN"); // Specify the actual endpoint where the bucket is located. The endpoint for EU-Dublin is used here as an example. For guidance on how to view the endpoint of a bucket, see https://support.huaweicloud.com/eu/usermanual-obs/obs_03_0312.html. String endPoint = "https://obs.eu-west-101.myhuaweicloud.eu"; // Obtain an endpoint using environment variables or import it in other ways. //String endPoint = System.getenv("ENDPOINT"); // Create an ObsClient instance. // Use the permanent AK/SK pair to initialize the client. ObsClient obsClient = new ObsClient(ak, sk,endPoint); // Use the temporary AK/SK pair and security token to initialize the client. // ObsClient obsClient = new ObsClient(ak, sk, securityToken, endPoint); try { // Download the object using streaming. ObsObject obsObject = obsClient.getObject("examplebucket", "objectname"); // Read the object content. System.out.println("Object content:"); InputStream input = obsObject.getObjectContent(); byte[] b = new byte[1024]; ByteArrayOutputStream bos = new ByteArrayOutputStream(); int len; while ((len = input.read(b)) != -1) { bos.write(b, 0, len); } System.out.println("getObjectContent successfully"); System.out.println(new String(bos.toByteArray())); bos.close(); input.close(); } catch (ObsException e) { System.out.println("getObjectContent failed"); // Request failed. Print the HTTP status code. System.out.println("HTTP Code:" + e.getResponseCode()); // Request failed. Print the server-side error code. System.out.println("Error Code:" + e.getErrorCode()); // Request failed. Print the error details. System.out.println("Error Message:" + e.getErrorMessage()); // Request failed. Print the request ID. System.out.println("Request ID:" + e.getErrorRequestId()); System.out.println("Host ID:" + e.getErrorHostId()); e.printStackTrace(); } catch (Exception e) { System.out.println("getObjectContent failed"); // Print other error information. e.printStackTrace(); } } } |
Feedback
Was this page helpful?
Provide feedbackThank you very much for your feedback. We will continue working to improve the documentation.