import com.aliyun.oss.model.PutObjectResult;
/**
* OSS方法测试类
* @author ZD
*
*/
public class OssTest {
//创建OSS客户端
private static OSSClient client;
//设置连接
private static String bucket_name = " your bucketName ";
private static String endpoint = " you endpoint ";
private static String accessKeyId = " you accessKeyId ";
private static String accessKeySecret = " you accessKeySecret ";
/**
* 配置Conf连接属性
* @return conf
*/
public ClientConfiguration conf() {
// 创建ClientConfiguration实例
ClientConfiguration conf = new ClientConfiguration();
// 设置OSSClient使用的最大连接数,默认1024
conf.setMaxConnections(200);
// 设置请求超时时间,默认50秒
conf.setSocketTimeout(10000);
// 设置失败请求重试次数,默认3次
conf.setMaxErrorRetry(3);
return conf;
}
/**
* 开启OSS
* @return boolean
*/
public boolean open() {
if(client == null) {
client = new OSSClient(endpoint, accessKeyId, accessKeySecret, conf());
}
return true;
}
/**
* 关闭OSS
* @return boolean
*/
public boolean close() {
if (client != null) {
client.shutdown();
}
return true;
}
/**
* 列出当前Bucket下的所有文件
*/
public void listObjects() {
ObjectListing objectListing = client.listObjects(bucket_name);
for (OSSObjectSummary objectSummary : objectListing.getObjectSummaries()) {
System.out.println(" - " + objectSummary.getKey() + " " + "(size = " + objectSummary.getSize() + ")");
}
}
/**
* 生成URL
*/
public void makeUrl() {
try {
GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucket_name,"UAT/upload_test_001.xml",
HttpMethod.GET);
// 设置URL过期时间为1小时
Date expiration = new Date(new Date().getTime() + 3600 * 1000);
// 生成URL签名(HTTP GET请求)
URL signedUrl = client.generatePresignedUrl(request);
System.out.println("signed url for getObject: " + signedUrl);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 上传文件
*/
public void upload(){
try {
// 创建上传Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("application/octet-stream");
//上传本地文件
PutObjectResult rs = client.putObject(bucket_name, "UAT/upload_test_001.xml", new File("D:/test.xml"),meta);
System.out.println("上传成功");
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
System.out.println("上传失败");
}
}
/**
* 上传文件流
*/
public void OssUpload(InputStream inputStream,String fileName){
try {
// 创建上传Object的Metadata
ObjectMetadata meta = new ObjectMetadata();
meta.setContentType("application/octet-stream");
client.putObject(bucket_name,fileName,inputStream,meta);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
/**
* 下载文件到本地
*/
public void download(){
try{
client.getObject(new GetObjectRequest(bucket_name, "UAT/upload_test_001.xml"), new File("D:/upload_test_001.xml"));
System.out.println("下载成功");
}catch(Exception e) {
e.printStackTrace();
System.out.println("下载失败");
}
}
/**
* 流式下载
*/
public InputStream ossDownload(){
OSSObject ossObject = client.getObject(bucket_name, "UAT/upload_test_001.xml");
InputStream is = new DataInputStream(ossObject.getObjectContent());
return is;
}
/**
* Oss删除
* @param name
*/
public void delete(String name) {
client.deleteObject(bucket_name, name);
}
}
更多OSS对象存储学习,请参考阿里云对象存储OSS_SDK文档