49 lines
1.7 KiB
Java
49 lines
1.7 KiB
Java
package com.muyu.javacomplier;
|
|
|
|
import com.aliyun.oss.OSSClient;
|
|
import com.aliyun.oss.model.GetObjectRequest;
|
|
import com.aliyun.oss.model.ObjectMetadata;
|
|
import com.muyu.common.core.domain.Result;
|
|
|
|
import java.io.File;
|
|
|
|
|
|
public class OssDownload {
|
|
|
|
// 阿里云 endpoint
|
|
private static final String endpoint = "https://oss-cn-shanghai.aliyuncs.com";
|
|
|
|
private static final String accessKeyId = "LTAI5t8LmMHfW8ckPaAZR6oQ";
|
|
|
|
private static final String accessKeySecret = "pwzEwkpxcTFgurkARyr7sG7V6syc9x";
|
|
|
|
private static final String bucketName = "011811";
|
|
// OSS文件路径
|
|
private static final String filePath = "build/rule/version/";
|
|
|
|
public static Result<Object> streamingDownload(String fileName) {
|
|
String source = null;
|
|
// 创建 OSSClient 实例
|
|
OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);
|
|
// 判断 bucket 是否存在
|
|
Boolean flag_bucket = ossClient.doesBucketExist(bucketName);
|
|
if (!flag_bucket) {
|
|
System.out.println("bucket不存在");
|
|
return Result.error("bucket不存在");
|
|
}
|
|
String objectName = fileName + ".java";
|
|
Boolean flag_file = ossClient.doesObjectExist(bucketName, filePath+objectName);
|
|
if (!flag_file) {
|
|
System.out.println("预下载文件不存在");
|
|
return Result.error("预下载文件不存在");
|
|
}
|
|
// 本地文件下载路径
|
|
String localPath = "home/"+objectName;
|
|
ObjectMetadata object = ossClient.getObject(new GetObjectRequest(bucketName, filePath + objectName), new File(localPath));
|
|
System.out.println(object);
|
|
ossClient.shutdown();
|
|
return Result.success(source);
|
|
}
|
|
|
|
}
|