华为obs的api上传方式的post方式,用java代码怎么写
时间: 2024-02-12 11:07:50 浏览: 119
您好,以下是使用 Java 代码实现华为 OBS API 的文件上传(POST)方式的示例代码:
```java
import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.InvalidKeyException;
import java.security.NoSuchAlgorithmException;
import javax.crypto.Mac;
import javax.crypto.spec.SecretKeySpec;
import org.apache.commons.codec.binary.Base64;
import org.apache.http.HttpEntity;
import org.apache.http.HttpHeaders;
import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.mime.MultipartEntityBuilder;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;
public class HuaweiOBSAPI {
private static final String ACCESS_KEY_ID = "YOUR_ACCESS_KEY_ID";
private static final String SECRET_ACCESS_KEY = "YOUR_SECRET_ACCESS_KEY";
private static final String ENDPOINT = "https://YOUR_ENDPOINT";
public static void main(String[] args) throws Exception {
String bucketName = "YOUR_BUCKET_NAME";
String objectKey = "YOUR_OBJECT_KEY";
File file = new File("PATH_TO_YOUR_FILE");
String url = getUrl(bucketName, objectKey);
HttpPost post = new HttpPost(url);
// 计算 Authorization
String auth = getAuthorization(post, bucketName, objectKey, file.length());
post.setHeader(HttpHeaders.AUTHORIZATION, auth);
// 构造请求体
HttpEntity entity = MultipartEntityBuilder.create()
.addBinaryBody("file", file, ContentType.DEFAULT_BINARY, objectKey)
.build();
post.setEntity(entity);
// 发送请求
HttpClient client = HttpClients.createDefault();
HttpResponse response = client.execute(post);
// 处理响应
int statusCode = response.getStatusLine().getStatusCode();
String responseBody = EntityUtils.toString(response.getEntity(), "UTF-8");
System.out.println(statusCode + ": " + responseBody);
}
private static String getUrl(String bucketName, String objectKey) {
return ENDPOINT + "/" + bucketName + "/" + objectKey;
}
private static String getAuthorization(HttpPost post, String bucketName, String objectKey, long contentLength)
throws NoSuchAlgorithmException, InvalidKeyException {
String verb = post.getMethod();
String contentMD5 = "";
String contentType = "";
long date = System.currentTimeMillis();
String canonicalizedOBSHeaders = "";
String canonicalizedResource = "/" + bucketName + "/" + objectKey;
String stringToSign = verb + "\n" + contentMD5 + "\n" + contentType + "\n" + date + "\n" +
canonicalizedOBSHeaders + canonicalizedResource;
byte[] signature = hmacSHA1(SECRET_ACCESS_KEY, stringToSign);
String auth = "OBS " + ACCESS_KEY_ID + ":" + new String(Base64.encodeBase64(signature));
return auth;
}
private static byte[] hmacSHA1(String key, String data) throws NoSuchAlgorithmException, InvalidKeyException {
Mac mac = Mac.getInstance("HmacSHA1");
SecretKeySpec keySpec = new SecretKeySpec(key.getBytes(), "HmacSHA1");
mac.init(keySpec);
return mac.doFinal(data.getBytes());
}
}
```
其中,需要将示例代码中的 `YOUR_ACCESS_KEY_ID`、`YOUR_SECRET_ACCESS_KEY`、`YOUR_ENDPOINT`、`YOUR_BUCKET_NAME`、`YOUR_OBJECT_KEY` 和 `PATH_TO_YOUR_FILE` 替换为您的华为云 OBC API 访问密钥、OBS 端点、桶名、对象名和本地文件路径。
另外,示例代码中使用了 Apache HttpClient 和 Apache Commons Codec 库,您需要将它们添加到您的项目依赖中。
阅读全文