Java Code Examples for com.aliyun.oss.model.ObjectMetadata#setContentLength()

The following examples show how to use com.aliyun.oss.model.ObjectMetadata#setContentLength() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: AliyunStorage.java    From BigDataPlatform with GNU General Public License v3.0 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }

}
 
Example 2
Source File: AliyunStorage.java    From dts-shop with GNU Lesser General Public License v3.0 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
	try {
		logger.info("阿里云存储OSS对象 内容长度:{},文件类型:{},KeyName:{}",contentLength,contentType,keyName);
		// 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
		ObjectMetadata objectMetadata = new ObjectMetadata();
		objectMetadata.setContentLength(contentLength);
		objectMetadata.setContentType(contentType);
		
		// 对象键(Key)是对象在存储桶中的唯一标识。
		PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
		
		PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
		if (putObjectResult != null && putObjectResult.getResponse() != null) {
			logger.info("阿里云存储结果code:" + putObjectResult.getResponse().getStatusCode());
		}
	} catch (Exception ex) {
		logger.error("阿里云存储 keyName:{} ,失败:{}",keyName,ex.getMessage());
		ex.printStackTrace();
	}

}
 
Example 3
Source File: AliyunOssClient.java    From markdown-image-kit with MIT License 6 votes vote down vote up
/**
 * Upload string.
 *
 * @param ossClient the ossClient client
 * @param instream  the instream
 * @param fileName  the file name
 * @return the string
 */
public String upload(@NotNull OSS ossClient,
                     @NotNull InputStream instream,
                     @NotNull String fileName) {
    try {
        // 创建上传 Object 的 Metadata
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(instream.available());
        objectMetadata.setCacheControl("no-cache");
        objectMetadata.setHeader("Pragma", "no-cache");
        objectMetadata.setContentType(ImageUtils.getImageType(fileName));
        objectMetadata.setContentDisposition("inline;filename=" + fileName);
        ossClient.putObject(bucketName, filedir + fileName, instream, objectMetadata);
        return getUrl(ossClient, filedir, fileName);
    } catch (IOException | OSSException | ClientException e) {
        log.trace("", e);
    }
    return "";
}
 
Example 4
Source File: AliyunStorage.java    From mall with MIT License 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        ex.printStackTrace();
    }

}
 
Example 5
Source File: AliyunStorage.java    From litemall with MIT License 6 votes vote down vote up
/**
 * 阿里云OSS对象存储简单上传实现
 */
@Override
public void store(InputStream inputStream, long contentLength, String contentType, String keyName) {
    try {
        // 简单文件上传, 最大支持 5 GB, 适用于小文件上传, 建议 20M以下的文件使用该接口
        ObjectMetadata objectMetadata = new ObjectMetadata();
        objectMetadata.setContentLength(contentLength);
        objectMetadata.setContentType(contentType);
        // 对象键(Key)是对象在存储桶中的唯一标识。
        PutObjectRequest putObjectRequest = new PutObjectRequest(bucketName, keyName, inputStream, objectMetadata);
        PutObjectResult putObjectResult = getOSSClient().putObject(putObjectRequest);
    } catch (Exception ex) {
        logger.error(ex.getMessage(), ex);
    }

}
 
Example 6
Source File: AliyunStorageBizServiceImpl.java    From unimall with Apache License 2.0 5 votes vote down vote up
@Override
public String upload(String fileName, InputStream is, long contentLength, String contentType) {
    ObjectMetadata objectMetadata = new ObjectMetadata();
    objectMetadata.setContentLength(contentLength);
    objectMetadata.setContentType(contentType);
    PutObjectRequest putObjectRequest = new PutObjectRequest(bucket, fileName, is, objectMetadata);
    ossClient.putObject(putObjectRequest);
    return baseUrl + fileName;
}
 
Example 7
Source File: AliOSSBlobStore.java    From multiapps-controller with Apache License 2.0 5 votes vote down vote up
private ObjectMetadata createObjectMetadataFromBlob(Blob blob) {
    ObjectMetadata objectMetadata = new ObjectMetadata();
    ContentMetadata blobContentMetadata = blob.getMetadata()
                                              .getContentMetadata();
    if (blobContentMetadata.getCacheControl() != null) {
        objectMetadata.setCacheControl(blobContentMetadata.getCacheControl());
    }
    if (blobContentMetadata.getContentDisposition() != null) {
        objectMetadata.setContentDisposition(blobContentMetadata.getContentDisposition());
    }
    if (blobContentMetadata.getContentEncoding() != null) {
        objectMetadata.setContentEncoding(blobContentMetadata.getContentEncoding());
    }
    if (blobContentMetadata.getContentLength() != null) {
        objectMetadata.setContentLength(blobContentMetadata.getContentLength());
    }
    if (blobContentMetadata.getContentType() != null) {
        objectMetadata.setContentType(blobContentMetadata.getContentType());
    }
    if (blobContentMetadata.getExpires() != null) {
        objectMetadata.setExpirationTime(blobContentMetadata.getExpires());
    }
    Date lastModified = blob.getMetadata()
                            .getLastModified();
    if (lastModified != null) {
        objectMetadata.setLastModified(lastModified);
    }
    if (blob.getAllHeaders() != null) {
        blob.getAllHeaders()
            .asMap()
            .forEach(objectMetadata::setHeader);
    }
    Map<String, String> userMetadata = blob.getMetadata()
                                           .getUserMetadata();
    if (userMetadata != null) {
        objectMetadata.setUserMetadata(userMetadata);
    }
    return objectMetadata;
}
 
Example 8
Source File: OssUploadServiceImpl.java    From mysiteforme with Apache License 2.0 4 votes vote down vote up
@Override
public String upload(MultipartFile file) throws IOException, NoSuchAlgorithmException {
    String fileName =null,realNames = "";
    StringBuffer returnUrl = new StringBuffer(getUploadInfo().getOssBasePath());
    String ossDir = getUploadInfo().getOssDir();
    try {
        fileName = file.getOriginalFilename();
        //上传文件
        StringBuffer realName = new StringBuffer(UUID.randomUUID().toString());
        String fileExtension = fileName.substring(fileName.lastIndexOf("."));
        realName.append(fileExtension);
        QETag tag = new QETag();
        String hash = tag.calcETag(file);
        Rescource rescource = new Rescource();
        EntityWrapper<RestResponse> wrapper = new EntityWrapper<>();
        wrapper.eq("hash",hash);
        wrapper.eq("source","oss");
        rescource = rescource.selectOne(wrapper);
        if( rescource!= null){
            return rescource.getWebUrl();
        }

        InputStream is = file.getInputStream();

        Long fileSize = file.getSize();
        //创建上传Object的Metadata
        ObjectMetadata metadata = new ObjectMetadata();
        metadata.setContentLength(is.available());
        metadata.setCacheControl("no-cache");
        metadata.setHeader("Pragma", "no-cache");
        metadata.setContentEncoding("utf-8");
        metadata.setContentType(ToolUtil.getContentType(fileName));
        StringBuffer description = new StringBuffer("filename/filesize=");
        description.append(realNames).append("/").append(fileSize).append("Byte.");
        metadata.setContentDisposition(description.toString());

        StringBuffer key = new StringBuffer();
        if(ossDir != null && !"".equals(ossDir)){
            key.append(ossDir).append("/");
            returnUrl.append(ossDir).append("/");
        }
        key.append(realName);
        returnUrl.append(realName);
        PutObjectResult putResult = getOSSClient().putObject(getUploadInfo().getOssBucketName(), key.toString(), is, metadata);
        //解析结果
        System.out.println("md5码为"+putResult.getETag());
        rescource = new Rescource();
        rescource.setFileName(realName.toString());
        rescource.setFileSize(new java.text.DecimalFormat("#.##").format(file.getSize()/1024)+"kb");
        rescource.setHash(hash);
        rescource.setFileType(StringUtils.isBlank(fileExtension)?"unknown":fileExtension);
        rescource.setWebUrl(returnUrl.toString());
        rescource.setSource("oss");
        rescource.insert();
        getOSSClient().shutdown();
        is.close();
    } catch (Exception e) {
        throw new MyException("上传阿里云OSS服务器异常." + e.getMessage());
    }
    return returnUrl.toString();
}