引入maven架包
<dependency>
<groupId>com.aliyun.oss</groupId>
<artifactId>aliyun-sdk-oss</artifactId>
<version>3.10.2</version>
</dependency>
import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.aliyun.oss.model.PutObjectResult;
import java.io.File;
public class OSSUploader {
private static final String endpoint = "yourOSSEndpoint";
private static final String accessKeyId = "yourAccessKeyId";
private static final String accessKeySecret = "yourAccessKeySecret";
private static final String bucketName = "yourBucketName";
private String roleArm;
public static void main(String[] args) {
OSS ossClient = new OSSClientBuilder().build(endpoint, accessKeyId, accessKeySecret);
String objectName = "yourObjectName";
String filePath = "yourFilePath";
File file = new File(filePath);
PutObjectResult result = ossClient.putObject(bucketName, objectName, file);
ossClient.shutdown();
}
public String getOSSUpload() {
Map<String, String> map = new HashMap<>();
map.put("appKeyId", accessKeyId );
map.put("appSecret", accessKeySecret );
String stsToken = null;
String endpoint = "sts.cn-hangzhou.aliyuncs.com";
String roleSessionName = "sessiontest";
String policy = "{\n" +
" \"Version\": \"1\", \n" +
" \"Statement\": [\n" +
" {\n" +
" \"Action\": [\n" +
" \"oss:PutObject\"\n" +
" ], \n" +
" \"Resource\": [\n" +
" \"acs:oss:*:*:examplebucket/*\" \n" +
" ], \n" +
" \"Effect\": \"Allow\"\n" +
" }\n" +
" ]\n" +
"}";
ProtocolType protocolType = ProtocolType.HTTPS;
try {
DefaultProfile.addEndpoint("", "cn-hangzhou", "Sts", endpoint);
IClientProfile profile = DefaultProfile.getProfile("cn-hangzhou", accessKeyId , accessKeySecret );
DefaultAcsClient client = new DefaultAcsClient(profile);
final AssumeRoleRequest request = new AssumeRoleRequest();
request.setMethod(MethodType.POST);
request.setRoleArn(roleArm);
request.setRoleSessionName(roleSessionName);
request.setPolicy(policy);
request.setProtocol(protocolType);
final AssumeRoleResponse response = client.getAcsResponse(request);
log.info("=============" + response.getCredentials().getAccessKeyId());
log.info("=============" + response.getCredentials().getAccessKeySecret());
log.info("=============" + response.getCredentials().getSecurityToken());
stsToken = response.getCredentials().getSecurityToken();
} catch (ClientException e) {
e.printStackTrace();
}
return stsToken;
}
public void getOSSSign() {
Map<String, String> respMap = new LinkedHashMap<>();
String host = "https://" + "videodis" + "." + endpoint ;
String callbackUrl = "";
String dir = "";
OSSClient client = new OSSClient(endpoint, accessKeyId , accessKeySecret);
try {
long expireTime = 60 * 30;
long expireEndTime = System.currentTimeMillis() + expireTime * 1000;
java.sql.Date expiration = new java.sql.Date(expireEndTime);
PolicyConditions policyConditions = new PolicyConditions();
policyConditions.addConditionItem(PolicyConditions.COND_CONTENT_LENGTH_RANGE, 0, 1048576000);
policyConditions.addConditionItem(MatchMode.StartWith, PolicyConditions.COND_KEY, dir);
String postPolicy = client.generatePostPolicy(expiration, policyConditions);
byte[] binaryData = postPolicy.getBytes(StandardCharsets.UTF_8);
String encodedPolicy = BinaryUtil.toBase64String(binaryData);
String postSignature = client.calculatePostSignature(postPolicy);
respMap.put("accessid", accessKeyId);
respMap.put("policy", encodedPolicy);
respMap.put("signature", postSignature);
respMap.put("dir", dir);
respMap.put("host", host);
respMap.put("expire", String.valueOf(expireEndTime / 1000));
JSONObject jasonCallback = new JSONObject();
jasonCallback.put("callbackUrl", callbackUrl);
jasonCallback.put("callbackBody",
"filename=${object}&size=${size}&mimeType=${mimeType}&height=${imageInfo.height}&width=${imageInfo.width}");
jasonCallback.put("callbackBodyType", "application/x-www-form-urlencoded");
String base64CallbackBody = BinaryUtil.toBase64String(jasonCallback.toString().getBytes());
respMap.put("callback", base64CallbackBody);
respMap.put("bucket", "videodis");
log.info("OSS结果:"+JSONObject.toJSONString(respMap));
} catch (Exception e) {
log.info(e.getMessage());
}
}
public void getOSSDownloadUrl(String bucket, String key) {
OSSClient client = new OSSClient(endpoint, accessKeyId , accessKeySecret );
Date expiration = new Date(System.currentTimeMillis() + 3600L * 1000 * 24 * 10 * 365);
URL url = client.generatePresignedUrl(bucket, key, expiration);
client.shutdown();
String s = url.toString().substring(0, url.toString().indexOf("?"));
if (s.contains("-internal")) {
s = s.replace("-internal", "");
}
log.info(s);
}
}