阿里雲oss簡單的上傳下載刪除(java)
阿新 • • 發佈:2020-12-27
技術標籤:Java日記
阿里雲oss上傳和下載。
配置maven
<!-- https://mvnrepository.com/artifact/com.aliyun.oss/aliyun-sdk-oss --> <dependency> <groupId>com.aliyun.oss</groupId> <artifactId>aliyun-sdk-oss</artifactId> <version>${aliyun-sdk-oss.version}</version> </dependency>
<aliyun-sdk-oss.version>3.11.1</aliyun-sdk-oss.version>
(1) AliyunOSSUtil.java
package com.game.common.tool.oss; import java.io.BufferedReader; import java.io.ByteArrayInputStream; import java.io.File; import java.io.IOException; import java.io.InputStreamReader; import java.util.ResourceBundle; import com.aliyun.oss.ClientException; import com.aliyun.oss.OSS; import com.aliyun.oss.OSSClientBuilder; import com.aliyun.oss.OSSException; import com.aliyun.oss.model.GetObjectRequest; import com.aliyun.oss.model.OSSObject; /** * 阿里雲oss簡單的上傳下載和刪除 * 參考文件:https://help.aliyun.com/document_detail/84781.html?spm=a2c4g.11174283.6. * 947.23d47da2vBI9co * * @author leng * */ public class AliyunOSSUtil { private static String endpoint; private static String accessKeyId; private static String accessKeySecret; private static String bucketName; static { ResourceBundle rb = ResourceBundle.getBundle("aliyunoss"); endpoint = rb.getString("aliyun.oss.endpoint"); accessKeyId = rb.getString("aliyun.oss.accessKeyId"); accessKeySecret = rb.getString("aliyun.oss.accessKeySecret"); bucketName = rb.getString("aliyun.oss.bucketName"); } /** * 上傳字串 * * @param key * @param content */ public static void upload(String key, String content) { OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { client.putObject(bucketName, key, new ByteArrayInputStream(content.getBytes())); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message: " + oe.getErrorMessage()); System.out.println("Error Code: " + oe.getErrorCode()); System.out.println("Request ID: " + oe.getRequestId()); System.out.println("Host ID: " + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ce.getMessage()); } finally { client.shutdown(); } } /** * 上傳檔案 * * @param key * @param content */ public static void upload(String key, File file) { OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { client.putObject(bucketName, key, file); } catch (OSSException oe) { System.out.println("Caught an OSSException, which means your request made it to OSS, " + "but was rejected with an error response for some reason."); System.out.println("Error Message: " + oe.getErrorMessage()); System.out.println("Error Code: " + oe.getErrorCode()); System.out.println("Request ID: " + oe.getRequestId()); System.out.println("Host ID: " + oe.getHostId()); } catch (ClientException ce) { System.out.println("Caught an ClientException, which means the client encountered " + "a serious internal problem while trying to communicate with OSS, " + "such as not being able to access the network."); System.out.println("Error Message: " + ce.getMessage()); } finally { client.shutdown(); } } /** * 顯示文字 * * @param key * @throws IOException */ public static void displayTextInput(String key) throws IOException { OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { OSSObject object = client.getObject(new GetObjectRequest(bucketName, key)); // System.out.println("Content-Type: " + // object.getObjectMetadata().getContentType()); BufferedReader reader = new BufferedReader(new InputStreamReader(object.getObjectContent())); while (true) { String line = reader.readLine(); if (line == null) break; System.out.println("\t" + line); } System.out.println(); reader.close(); } finally { client.shutdown(); } } /** * 判斷是否存在 * * @param key * @return */ public static boolean exist(String key) { OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { boolean found = client.doesObjectExist(bucketName, key); return found; } finally { client.shutdown(); } } /** * 刪除 * * @param key * @return */ public static void delete(String key) { OSS client = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret); try { client.deleteObject(bucketName, key); } finally { client.shutdown(); } } public static void main(String[] args) { upload("3.txt", "aaaa"); System.out.println("----"); } }
(2) aliyunoss.properties
aliyun.oss.endpoint=http://oss-cn-beijing.aliyuncs.com
aliyun.oss.accessKeyId=xxx
aliyun.oss.accessKeySecret=xxxx
aliyun.oss.bucketName=xxxx