public PutObjectTask(String bucketName, String objectKey, String contentType) { super(HttpMethod.PUT); this.objectKey = objectKey; this.bucketName = bucketName; objectMetaData = new ObjectMetaData(); objectMetaData.setContentType(contentType); }
/** 参数合法性验证 */ @Override protected void checkArguments() { if (Helper.isEmptyString(bucketName) || Helper.isEmptyString(objectKey)) { throw new IllegalArgumentException("bucketName or objectKey not set"); } if (objectMetaData == null || Helper.isEmptyString(objectMetaData.getContentType())) { throw new IllegalArgumentException("ObjectMetaData not properly set"); } }
/** 构造HttpPut */ protected HttpUriRequest generateHttpRequest() { // 生成Http请求 String resource = httpTool.generateCanonicalizedResource("/" + bucketName + "/" + objectKey); String requestUri = OSS_END_POINT + resource; HttpPut httpPut = new HttpPut(requestUri); // 构造HttpPut String dateStr = Helper.getGMTDate(); String xossHeader = OSSHttpTool.generateCanonicalizedHeader(objectMetaData.getAttrs()); String authorization = OSSHttpTool.generateAuthorization( accessId, accessKey, httpMethod.toString(), "", objectMetaData.getContentType(), dateStr, xossHeader, resource); httpPut.setHeader(AUTHORIZATION, authorization); httpPut.setHeader(DATE, dateStr); httpPut.setHeader(HOST, OSS_HOST); OSSHttpTool.addHttpRequestHeader(httpPut, CACHE_CONTROL, objectMetaData.getCacheControl()); OSSHttpTool.addHttpRequestHeader( httpPut, CONTENT_DISPOSITION, objectMetaData.getContentDisposition()); OSSHttpTool.addHttpRequestHeader( httpPut, CONTENT_ENCODING, objectMetaData.getContentEncoding()); OSSHttpTool.addHttpRequestHeader(httpPut, CONTENT_TYPE, objectMetaData.getContentType()); OSSHttpTool.addHttpRequestHeader( httpPut, EXPIRES, Helper.getGMTDate(objectMetaData.getExpirationTime())); // 加入用户自定义header for (Entry<String, String> entry : objectMetaData.getAttrs().entrySet()) { OSSHttpTool.addHttpRequestHeader(httpPut, entry.getKey(), entry.getValue()); } if (this.data != null && this.data.length > 0) { httpPut.setEntity(new ByteArrayEntity(this.data)); } return httpPut; }