preface
Ali cloud OSS about the picture upload set expiration time problem, puzzled me a few hours of time, before is not used Ali OSS, but this development found that the original method can not upload, after the code uploaded by Baidu, the picture has expiration time, this TM really confused me.After I carefully compared before using Ali OSS, I found that the read and write permissions of my Bucket Settings were == private == After changing back to == public read ==, there is no need to set the expiration time I hope you guys don’t have this problem again!
1. Import dependencies
<! -- Ali Cloud OSS-->
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
<! -- Date toolbar dependencies -->
<dependency>
<groupId>joda-time</groupId>
<artifactId>joda-time</artifactId>
<version>2.9.3</version>
</dependency>
Copy the code
2. You want to set the expiration time
Set the read and write permissions of the Bucket to == private ==
@Service
public class OssServiceImpl implements OssService {
//inputStream: file.getinputStream ();
//module: specifies the folder
//originalFilename: file name file.getoriginalfilename ()
@Override
public String uploadFileAvatar(InputStream inputStream,String module,String originalFilename) {
// Utility class gets the value
String endpoint = ConstantPropertiesUtils.END_POINT;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String folder = new DateTime().toString("yyyy/MM/dd");
String fileName = UUID.randomUUID().toString();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
// The folder name in oss
String objectName = module + "/" + folder + "/" + fileName + fileExtension;
HTTP headers can be set through the file meta information (this can only be directly accessed through the returned link).
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("image/jpg");
// File upload
ossClient.putObject(bucketName, objectName, inputStream,objectMetadata);
// Set the URL expiration time to 1 hour.
Date expiration = new Date(System.currentTimeMillis() + 3600 * 100000);
String url = ossClient.generatePresignedUrl(bucketName, objectName, expiration).toString();
returnurl; }}Copy the code
3. Do not want to set expiration time
Set the read and write permissions of Bucket to == public read ==
@Service
public class OssServiceImpl implements OssService {
//inputStream: file.getinputStream ();
//module: specifies the folder
//originalFilename: file name file.getoriginalfilename ()
@Override
public String uploadFileAvatar(InputStream inputStream,String module,String originalFilename) {
// Utility class gets the value
String endpoint = ConstantPropertiesUtils.END_POINT;
String accessKeyId = ConstantPropertiesUtils.ACCESS_KEY_ID;
String accessKeySecret = ConstantPropertiesUtils.ACCESS_KEY_SECRET;
String bucketName = ConstantPropertiesUtils.BUCKET_NAME;
// Create an OSS instance
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String folder = new DateTime().toString("yyyy/MM/dd");
String fileName = UUID.randomUUID().toString();
String fileExtension = originalFilename.substring(originalFilename.lastIndexOf("."));
// The folder name in oss
String objectName = module + "/" + folder + "/" + fileName + fileExtension;
HTTP headers can be set through the file meta information (this can only be directly accessed through the returned link).
ObjectMetadata objectMetadata = new ObjectMetadata();
objectMetadata.setContentType("image/jpg");
ossClient.putObject(bucketName, objectName, inputStream,objectMetadata);
// Close OSSClient.
ossClient.shutdown();
String url = "http://"+bucketName+"."+endpoint+"/"+objectName;
returnurl; }}Copy the code
That’s the end of this blog post. If you feel good about it, you can connect to it three times with one button! Thanks for your support!!
Java Learning path directory index