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 API Reference
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 a browser-based upload. You can use code to simulate a browser-based upload. For details, see PostObjectSample. You can also perform a browser-based upload as follows:
- Call ObsClient.createPostSignature to generate request parameters for authentication.
- Prepare an HTML form page.
- Enter the request parameters in the HTML page.
- Select a local file to and upload it in browser-based mode.
There are two request parameters generated:
- policy, which corresponds to the policy field in the form
- signature, which corresponds to the signature field in the form
The following sample code shows how to generate the request parameters in a browser-based upload.
String endPoint = "http://your-endpoint";
String ak = "*** Provide your Access Key ***";
String sk = "*** Provide your Secret Key ***";
// Create an instance of ObsClient.
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.
System.out.println("\t" + response.getPolicy());
System.out.println("\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.
- You can directly download the HTML form example: PostDemo.
Last Article: Performing a Resumable Upload
Next Article: Object Download
Did this article solve your problem?
Thank you for your score!Your feedback would help us improve the website.