Help Center/ Object Storage Service/ SDK Reference/ Android/ Object Upload/ Performing a Browser-Based Upload
Updated on 2024-06-21 GMT+08:00

Performing a Browser-Based Upload

If you have any questions during development, post them on the Issues page of GitHub. For details about parameters and usage of each API, see the .

Performing a browser-based upload is to upload objects to a specified bucket in HTML form. The maximum size of an object is 5 GB.

You can call ObsClient.createPostSignature to generate request parameters for browser-based upload. . You can also perform a browser-based upload.

  1. Call ObsClient.createPostSignature to generate request parameters for authentication.
  2. Prepare an HTML form page.
  3. Enter the request parameters in the HTML page.
  4. Select a local file and upload it in browser-based mode.

There are two request parameters generated:

  • policy, which corresponds to the policy field in the form
  • signature: corresponds to the signature field in the form.

The following sample code shows how to generate the parameters in a browser-based upload request.

// Hard-coded or plaintext AK/SK are risky. For security purposes, encrypt your AK/SK and store them in the configuration file or environment variables. In this example, the AK/SK are stored in environment variables for identity authentication. Before running this example, configure environment variables ACCESS_KEY_ID and SECRET_ACCESS_KEY_ID.
// Obtain an AK/SK pair on the management console. For details, see https://support.huaweicloud.com/eu/usermanual-ca/ca_01_0003.html.
String ak = System.getenv("ACCESS_KEY_ID");
String sk = System.getenv("SECRET_ACCESS_KEY_ID");
String endPoint = "https://your-endpoint";
// Create an ObsClient instance.
final ObsClient obsClient = new ObsClient(ak, sk, endPoint);

PostSignatureRequest request = new PostSignatureRequest();
// Fill in parameters in the form.
Map<String, Object> formParams = new HashMap<String, Object>();
// Set the object ACL to public-read.
formParams.put("x-obs-acl", "public-read");
// Set the MIME type for the object.
formParams.put("content-type", "text/plain");

request.setFormParams(formParams);
// Set the validity period for the browser-based upload request, in seconds.
request.setExpires(3600);
PostSignatureResponse response = obsClient.createPostSignature(request);

// Obtain the request parameters.
Log.i("CreatePostSignature", "\t" + response.getPolicy());
Log.i("CreatePostSignature", "\t" + response.getSignature());

Code of an HTML form example is as follows:

<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
</head>
<body>

<form action="http://bucketname.your-endpoint/" method="post" enctype="multipart/form-data">
Object key
<!-- Object name -->
<input type="text" name="key" value="objectname" />
<p>
ACL
<!-- Object ACL -->
<input type="text" name="x-obs-acl" value="public-read" />
<p>
Content-Type
<!-- Object MIME type -->
<input type="text" name="content-type" value="text/plain" />
<p>
<!-- Base64 code of the policy -->
<input type="hidden" name="policy" value="*** Provide your policy ***" />
<!-- AK -->
<input type="hidden" name="AccessKeyId" value="*** Provide your access key ***"/>
<!-- Signature information -->
<input type="hidden" name="signature" value="*** Provide your signature ***"/>

<input name="file" type="file" />
<input name="submit" value="Upload" type="submit" />
</form>
</body>
</html>
  • Values of policy and signature in the HTML form are obtained from the returned result of ObsClient.createPostSignature.