public static String xs3_generate_url(String xs3_objname, String content_type) { AWSCredentials xs3_credentials = new BasicAWSCredentials(xs3_access_key, xs3_secret_key); ClientConfiguration xs3_clientconfig = new ClientConfiguration(); xs3_clientconfig.setProtocol(Protocol.HTTP); S3ClientOptions xs3_client_options = new S3ClientOptions(); xs3_client_options.setPathStyleAccess(true); xs3_client = new AmazonS3Client(xs3_credentials, xs3_clientconfig); xs3_client.setEndpoint(xs3_endpoint); xs3_client.setS3ClientOptions(xs3_client_options); try { java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 5; expiration.setTime(milliSeconds); GeneratePresignedUrlRequest xs3_genurl_req = new GeneratePresignedUrlRequest(xs3_bucketname, xs3_objname); xs3_genurl_req.setMethod(HttpMethod.PUT); xs3_genurl_req.setExpiration(expiration); xs3_genurl_req.setContentType(content_type); xs3_genurl_req.addRequestParameter("x-amz-acl", "public-read"); URL url = xs3_client.generatePresignedUrl(xs3_genurl_req); System.out.println(url.toString()); return url.toString(); } catch (AmazonServiceException ase) { System.out.println("xs3_svr_error_message:" + ase.getMessage()); System.out.println("xs3_svr_status_code: " + ase.getStatusCode()); System.out.println("xs3_svr_error_code: " + ase.getErrorCode()); System.out.println("xs3_svr_error_type: " + ase.getErrorType()); System.out.println("xs3_svr_request_id: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("xs3_clt_error_message:" + ace.getMessage()); } return null; }
public static String xs3_init_multi_upload(String xs3_objname, int file_size, String file_type) { AWSCredentials xs3_credentials = new BasicAWSCredentials(xs3_access_key, xs3_secret_key); ClientConfiguration xs3_clientconfig = new ClientConfiguration(); xs3_clientconfig.setProtocol(Protocol.HTTP); S3ClientOptions xs3_client_options = new S3ClientOptions(); xs3_client_options.setPathStyleAccess(true); xs3_client = new AmazonS3Client(xs3_credentials, xs3_clientconfig); xs3_client.setEndpoint(xs3_endpoint); xs3_client.setS3ClientOptions(xs3_client_options); try { InitiateMultipartUploadRequest xs3_multi_req = new InitiateMultipartUploadRequest(xs3_bucketname, xs3_objname); xs3_multi_req.setCannedACL(CannedAccessControlList.PublicRead); ObjectMetadata xs3_meta = new ObjectMetadata(); xs3_meta.setContentType(file_type); xs3_multi_req.setObjectMetadata(xs3_meta); InitiateMultipartUploadResult xs3_multi_res = xs3_client.initiateMultipartUpload(xs3_multi_req); String xs3_multi_uploadid = xs3_multi_res.getUploadId(); String json_urls = gen_part_url(xs3_multi_uploadid, file_size, xs3_objname, file_type); return json_urls; } catch (AmazonServiceException ase) { System.out.println("xs3_svr_error_message:" + ase.getMessage()); System.out.println("xs3_svr_status_code: " + ase.getStatusCode()); System.out.println("xs3_svr_error_code: " + ase.getErrorCode()); System.out.println("xs3_svr_error_type: " + ase.getErrorType()); System.out.println("xs3_svr_request_id: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("xs3_clt_error_message:" + ace.getMessage()); } return null; }
public static void xs3_coplete_multi_upload(String xs3_objname, String uploadId) { AWSCredentials xs3_credentials = new BasicAWSCredentials(xs3_access_key, xs3_secret_key); ClientConfiguration xs3_clientconfig = new ClientConfiguration(); xs3_clientconfig.setProtocol(Protocol.HTTP); S3ClientOptions xs3_client_options = new S3ClientOptions(); xs3_client_options.setPathStyleAccess(true); xs3_client = new AmazonS3Client(xs3_credentials, xs3_clientconfig); xs3_client.setEndpoint(xs3_endpoint); xs3_client.setS3ClientOptions(xs3_client_options); try { List<PartETag> rest_parts = listPartsXml(xs3_objname, uploadId); if (null == rest_parts) { return; } for (PartETag item : rest_parts) { System.out.println(item.getETag() + " -> " + item.getPartNumber()); } CompleteMultipartUploadRequest comp_req = new CompleteMultipartUploadRequest(xs3_bucketname, xs3_objname, uploadId, rest_parts); CompleteMultipartUploadResult comp_result = xs3_client.completeMultipartUpload(comp_req); System.out.println(comp_result.getETag()); System.out.println(comp_result.getKey()); } catch (AmazonServiceException ase) { System.out.println("xs3_svr_error_message:" + ase.getMessage()); System.out.println("xs3_svr_status_code: " + ase.getStatusCode()); System.out.println("xs3_svr_error_code: " + ase.getErrorCode()); System.out.println("xs3_svr_error_type: " + ase.getErrorType()); System.out.println("xs3_svr_request_id: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("xs3_clt_error_message:" + ace.getMessage()); } }
public static String gen_part_url( String uploadId, int file_size, String file_name, String file_type) { AWSCredentials xs3_credentials = new BasicAWSCredentials(xs3_access_key, xs3_secret_key); ClientConfiguration xs3_clientconfig = new ClientConfiguration(); xs3_clientconfig.setProtocol(Protocol.HTTP); S3ClientOptions xs3_client_options = new S3ClientOptions(); xs3_client_options.setPathStyleAccess(true); xs3_client = new AmazonS3Client(xs3_credentials, xs3_clientconfig); xs3_client.setEndpoint(xs3_endpoint); xs3_client.setS3ClientOptions(xs3_client_options); try { final int xs3_part_size = 1024 * 1024 * 5; int xs3_part_count = (int) Math.ceil((double) (file_size) / (double) xs3_part_size); JSONArray jsonArray = new JSONArray(); JSONObject jsonObject_1 = new JSONObject(); jsonObject_1.put("total_num", xs3_part_count); jsonObject_1.put("upload_id", uploadId); JSONArray jsonArray_sub = new JSONArray(); for (int part_no = 0; part_no < xs3_part_count; part_no++) { long xs3_offset_bytes = xs3_part_size * part_no; long part_size = xs3_part_size < (file_size - xs3_offset_bytes) ? xs3_part_size : (file_size - xs3_offset_bytes); java.util.Date expiration = new java.util.Date(); long milliSeconds = expiration.getTime(); milliSeconds += 1000 * 60 * 5; expiration.setTime(milliSeconds); GeneratePresignedUrlRequest xs3_genurl_req = new GeneratePresignedUrlRequest(xs3_bucketname, file_name); xs3_genurl_req.setMethod(HttpMethod.PUT); xs3_genurl_req.setExpiration(expiration); xs3_genurl_req.setContentType(file_type); xs3_genurl_req.addRequestParameter("uploadId", uploadId); xs3_genurl_req.addRequestParameter("partNumber", String.valueOf(part_no + 1)); URL url = xs3_client.generatePresignedUrl(xs3_genurl_req); System.out.println(url.toString()); JSONObject jsonObject = new JSONObject(); jsonObject.put("part_idx", part_no); jsonObject.put("part_url", url.toString()); jsonObject.put("upload_len", part_size); jsonObject.put("part_begin", xs3_offset_bytes); jsonObject.put("part_end", xs3_offset_bytes + part_size); jsonArray_sub.add(jsonObject); } jsonObject_1.put("multi_list", jsonArray_sub); jsonArray.add(jsonObject_1); return jsonArray.toString(); } catch (AmazonServiceException ase) { System.out.println("xs3_svr_error_message:" + ase.getMessage()); System.out.println("xs3_svr_status_code: " + ase.getStatusCode()); System.out.println("xs3_svr_error_code: " + ase.getErrorCode()); System.out.println("xs3_svr_error_type: " + ase.getErrorType()); System.out.println("xs3_svr_request_id: " + ase.getRequestId()); } catch (AmazonClientException ace) { System.out.println("xs3_clt_error_message:" + ace.getMessage()); } return null; }