开发者社区 问答 正文

oss 授权上传 安卓


现有第三方授权加密上传后的链接,但安卓端OSS Java SDK 为找到用 第三方授权加密上传链接进行上传的代码,有的话回复一下


import com.aliyun.oss.ClientConfiguration;
import com.aliyun.oss.HttpMethod;
import com.aliyun.oss.OSSClient;
import com.aliyun.oss.common.auth.CredentialsProviderFactory;
import com.aliyun.oss.model.GeneratePresignedUrlRequest;
import com.aliyun.oss.model.ListObjectsRequest;
import com.aliyun.oss.model.OSSObjectSummary;
import com.aliyun.oss.model.ObjectMetadata;

public static void main(String[] args) throws Exception {
  String sign = "http://watch-portal.oss-cn-beijing.aliyuncs.com/data/11/1151b7c82780dec9e24ce903047577db.jpg?Expires=1539165607&OSSAccessKeyId=LTAIVjDf6lDyVFcL&Signature=zvV6r3d9pUCiY0QZfWWOKx3qMhs%3D";
  String type = "image/jpeg";
  
  OSSClient client = new OSSClient(
   "demo",
   CredentialsProviderFactory.newDefaultCredentialProvider("demo", "demo"),
   new ClientConfiguration());
  Map<String, String> headers = new HashMap<String, String>();
  //headers.put(com.aliyun.oss.common.utils.HttpHeaders.CONTENT_TYPE, type);
  headers.put("Content-Type", type);
  client.putObject(new URL(sign), "E:/360截图16540604688794.png", headers, true);
  client.shutdown();
 }

展开
收起
上而求索 2018-10-10 16:01:32 1565 分享 版权
阿里云 AI 助理回答

在阿里云OSS Java SDK中,如果已经有了一个第三方授权加密后的上传链接(预签名URL),你不需要直接使用OSSClient进行上传操作。预签名URL允许你在没有直接提供AccessKey和SecretKey的情况下,通过HTTP PUT请求直接上传文件到指定的OSS路径。这意味着你可以利用Java的网络库(如HttpURLConnection或者第三方库如OkHttp)来执行这个PUT请求。

以下是一个简单的示例,展示如何使用Java原生的HttpURLConnection来通过预签名URL上传文件:

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;

public class UploadWithPresignedUrl {
    public static void main(String[] args) throws IOException {
        String presignedUrlStr = "http://watch-portal.oss-cn-beijing.aliyuncs.com/data/11/1151b7c82780dec9e24ce903047577db.jpg?Expires=1539165607&OSSAccessKeyId=yourAccessKeyId&Signature=yourSignature"; // 替换为你的预签名URL

        URL url = new URL(presignedUrlStr);
        HttpURLConnection connection = (HttpURLConnection) url.openConnection();
        connection.setDoOutput(true);
        connection.setRequestMethod("PUT");

        File file = new File("path_to_your_local_file"); // 替换为你要上传的本地文件路径
        try (InputStream fis = new FileInputStream(file)) {
            byte[] buffer = new byte[1024];
            int bytesRead;
            while ((bytesRead = fis.read(buffer)) != -1) {
                connection.getOutputStream().write(buffer, 0, bytesRead);
            }
        }

        int responseCode = connection.getResponseCode();
        if (responseCode == HttpURLConnection.HTTP_OK) {
            System.out.println("File uploaded successfully.");
        } else {
            System.out.println("Upload failed. Response Code: " + responseCode);
        }

        connection.disconnect();
    }
}

这段代码首先创建了一个到预签名URL的连接,并设置请求方法为PUT。然后,它读取本地文件的字节流并写入到连接的输出流中,完成文件上传。最后,根据响应码判断上传是否成功。

请确保替换presignedUrlStr中的URL为你实际的预签名URL,并且替换file变量对应的本地文件路径为你要上传的文件的实际路径。

有帮助
无帮助
AI 助理回答生成答案可能存在不准确,仅供参考
0 条回答
写回答
取消 提交回答