开发者社区> 问答> 正文

使用签名URL上传文件,返回报文成功,但找不到文件

已解决

使用以下代码:最后一个段落

使用签名URL上传文件

  1. // Endpoint以杭州为例,其它Region请按实际情况填写。
  2. String endpoint = "http://oss-cn-hangzhou.aliyuncs.com";
  3. // 阿里云主账号AccessKey拥有所有API的访问权限,风险很高。强烈建议您创建并使用RAM账号进行API访问或日常运维,请登录 https://ram.console.aliyun.com 创建RAM账号。
  4. String accessKeyId = "<yourAccessKeyId>";
  5. String accessKeySecret = "<yourAccessKeySecret>";
  6. String bucketName = "<yourBucketName>";
  7. String objectName = "<yourObjectName>";

  8. // 创建OSSClient实例。
  9. OSSClient ossClient = new OSSClient(endpoint, accessKeyId, accessKeySecret);

  10. // 生成签名URL。
  11. Date expiration = DateUtil.parseRfc822Date("Thu, 19 Mar 2019 18:00:00 GMT");
  12. GeneratePresignedUrlRequest request = new GeneratePresignedUrlRequest(bucketName, objectName, HttpMethod.PUT);
  13. // 设置过期时间。
  14. request.setExpiration(expiration);
  15. // 设置Content-Type。
  16. request.setContentType("application/octet-stream");
  17. // 添加用户自定义元信息。
  18. request.addUserMetadata("author", "aliy");
  19. // 生成签名URL(HTTP PUT请求)。
  20. URL signedUrl = ossClient.generatePresignedUrl(request);
  21. System.out.println("signed url for putObject: " + signedUrl);

  22. // 使用签名URL发送请求。
  23. File f = new File("<yourLocalFile>");
  24. FileInputStream fin = new FileInputStream(f);
  25. // 添加PutObject请求头。
  26. Map<String, String> customHeaders = new HashMap<String, String>();
  27. customHeaders.put("Content-Type", "application/octet-stream");
  28. customHeaders.put("x-oss-meta-author", "aliy");

  29. PutObjectResult result = ossClient.putObject(signedUrl, fin, f.length(), customHeaders);

  30. // 关闭OSSClient。
  31. ossClient.shutdown();
https://help.aliyun.com/document_detail/32016.html?spm=a2c4g.11186623.6.732.19c2c06dHSr4PW

正常返回报文,但是无法在管理控制台的目录下找到文件

1:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << HTTP/1.1 200 OK
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Server: AliyunOSS
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Date: Fri, 28 Sep 2018 13:38:25 GMT
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-Length: 0
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Connection: keep-alive
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << x-oss-request-id: 5BAE2ECF9497D1FE64A06CC1
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << ETag: "D57B05B8D92BDAA6CEE69259F37717E2"
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << x-oss-hash-crc64ecma: 4305414198231359716
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << Content-MD5: 1XsFuNkr2qbO5pJZ83cX4g==
21:38:29.280 [main] DEBUG org.apache.http.headers - http-outgoing-0 << x-oss-server-time: 130
21:38:29.290 [main] DEBUG o.a.h.impl.execchain.MainClientExec - Connection can be kept alive indefinitely


请问是什么问题 ?

展开
收起
1915713976271916 2018-09-28 21:46:23 1480 0
1 条回答
写回答
取消 提交回答
  • 采纳回答
    卡了我几个小时!我也是服气。解决了!
    主要由于   objectName 这个变量的问题,这里不光需要文件夹的路径,还需要设置好文件的名称,
    例如: xxx/xxx/xxx.zip 
    如果没有文件名称,也会上传成功,返回值一切正常,什么异常也不会返回的,但是控制台啥也没有。
    这里还要设置 31行 
    customHeaders . put ( "x-oss-meta-author" ,   "aliy" );

    设置成你创建的子账户用户名, 这个子用户说是要做一系列配置,这里就不详细交代了
    总之是被官方的代码给坑了,这也是无奈了。 感觉文档缺乏连续性,接入出一点问题都要研究好半天。
    2018-09-28 23:09:57
    赞同 展开评论 打赏
问答分类:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载