单个文件移动
使用官方的python Demo试了好久都是signature error,PHP返回400,最后用Java才成功的
需要更改四个参数:
package com.upyun.demo;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.security.InvalidKeyException;
import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.security.SignatureException;
import java.text.SimpleDateFormat;
import java.util.Base64;
import java.util.Calendar;
import java.util.Locale;
import java.util.TimeZone;
/***
* 同服务下复制或移动文件
*/
public class CopyOrMoveFileDemo {
private static final String HMAC_SHA1_ALGORITHM = "HmacSHA1";
private final static String host = "http://v0.api.upyun.com";
public static void main(String[] args) throws Exception {
//如果不记得操作员去"账号管理->操作员->操作员"查看
//如果不记得操作员密码去"账号管理->操作员->编辑->新密码->生成"重新生成密码
String operator = "操作员";
String secret = "操作员密码";
//下列两项都需要加上云存储的服务名称
//例如:
//"/zhangguapi/test/a.jpg"
//表示服务名称"zhangguapi"下的test文件夹里面的a.jpg文件
//"/zhangguapi/a.jpg"
//表示服务名称"zhangguapi"根目录下的a.jpg文件
String uri = "/服务名称/文件夹名/文件名"; //目标路径
String source = "/服务名称/文件名"; //源文件
// 选择复制或移动
// X-Upyun-Copy-Source 复制,
// X-Upyun-Move-Source 移动
String copyOrMove = "X-Upyun-Move-Source";
String date = getRfc1123Time();
System.out.println(copyorMoveFile(uri, source, date, copyOrMove, operator, secret));
}
private static Integer copyorMoveFile(String uri, String source, String date, String copyOrMove, String operator, String secret) throws Exception {
URL url = new URL(host + uri);
HttpURLConnection conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("PUT");
conn.setUseCaches(false);
conn.setDoOutput(true);
conn.setRequestProperty("Authorization", getAuth(operator, secret, "PUT", uri, date));
conn.setRequestProperty(copyOrMove, source);
conn.setRequestProperty("date", date);
conn.setRequestProperty("Content-Length", "0");
//创建连接
conn.connect();
OutputStream os = conn.getOutputStream();
int responseCode = conn.getResponseCode(); //返回的状态码
if (os != null) {
os.close();
}
if (conn != null) {
conn.disconnect();
}
return responseCode;
}
/**
* 获取签名
*
* @param operator
* @param secret
* @param method
* @param uri
* @param date
* @return 签名
*/
private static String getAuth(String operator, String secret, String method, String uri, String date) throws Exception {
return sign(operator, md5(secret), method, uri, date, "", "");
}
private static String md5(String string) {
byte[] hash;
try {
hash = MessageDigest.getInstance("MD5").digest(string.getBytes("UTF-8"));
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("UTF-8 is unsupported", e);
} catch (NoSuchAlgorithmException e) {
throw new RuntimeException("MessageDigest不支持MD5Util", e);
}
StringBuilder hex = new StringBuilder(hash.length * 2);
for (byte b : hash) {
if ((b & 0xFF) < 0x10) hex.append("0");
hex.append(Integer.toHexString(b & 0xFF));
}
return hex.toString();
}
private static byte[] hashHmac(String data, String key)
throws SignatureException, NoSuchAlgorithmException, InvalidKeyException {
SecretKeySpec signingKey = new SecretKeySpec(key.getBytes(), HMAC_SHA1_ALGORITHM);
Mac mac = Mac.getInstance(HMAC_SHA1_ALGORITHM);
mac.init(signingKey);
return mac.doFinal(data.getBytes());
}
public static String sign(String key, String secret, String method, String uri, String date, String policy,
String md5) throws Exception {
String value = method + "&" + uri;
if (date != "") {
value = value + "&" + date;
}
if (policy != "") {
value = value + "&" + policy;
}
if (md5 != "") {
value = value + "&" + md5;
}
byte[] hmac = hashHmac(value, secret);
System.out.println(value);
String sign = Base64.getEncoder().encodeToString(hmac);
return "UPYUN " + key + ":" + sign;
}
public static String getRfc1123Time() {
Calendar calendar = Calendar.getInstance();
SimpleDateFormat dateFormat = new SimpleDateFormat(
"EEE, dd MMM yyyy HH:mm:ss z", Locale.US);
dateFormat.setTimeZone(TimeZone.getTimeZone("GMT"));
return dateFormat.format(calendar.getTime());
}
}
批量移动
public static void main(String[] args) throws Exception {
// 数组里面的数据要跟uri和source拼接
String[] data ={"mmexport1633187782229",
"mmexport1633360628082",
"mmexport1633442423299",
"mmexport1633442453859",
"mmexport1633442456259",
"mmexport1633442459284",
"mmexport1633442461525"};
//operator:操作
String operator = "操作员";
String secret = "操作员密码";
/*
* 选择复制或移动
* X-Upyun-Copy-Source 复制,
* X-Upyun-Move-Source 移动
*/
String copyOrMove = "X-Upyun-Move-Source";
String date = getRfc1123Time();
for (int i = 0; i < data.length; i++) {
String uri = "/zhangguapi-other/picture/lulu/"+data[i]+".jpg"; //目标路径
String source = "/zhangguapi-other/picture/"+data[i]+".jpg"; //源文件
System.out.println(copyorMoveFile(uri, source, date, copyOrMove, operator, secret));
}
}