对象相关接口 请求参数 参数 类型 描述 bucketName String 桶名 key String 对象名 expiration Date 过期时间,默认900秒 method HttpMethod HTTP 方法,设置为 PUT 以生成上传 URL,默认为GET 返回结果 生成对应的预签名上传 URL,该链接允许用户在指定的时间内直接将对象上传到媒体存储存储桶。 生成预签名下载URL 功能说明 您可以利用 GeneratePresignedUrl 接口为一个对象生成一个预签名的URL链接。 代码示例 生成下载对象的预签名 URL: java public void generateGetPresignedUrl() throws AmazonClientException { System.out.println("generateGetPresignedUrl"); String bucket " "; String key " "; Date now new Date(); Calendar newTime Calendar.getInstance(); newTime.setTime(now); newTime.add(Calendar.SECOND, 900); Date expire newTime.getTime(); GeneratePresignedUrlRequest request new GeneratePresignedUrlRequest(bucket, key, HttpMethod.GET); request.withExpiration(expire); URL ret s3Client.generatePresignedUrl(request); System.out.println("generateGetPresignedUrl: " + ret); } 生成对应的下载预签名URL后,可以直接通过该URL下载对象: java public void getObjUsingPresignedUrl(String presignedUrl, String downloadPath) throws IOException { System.out.println("开始通过预签名 URL 下载文件..."); HttpURLConnection connection (HttpURLConnection) new URL(presignedUrl).openConnection(); connection.setRequestMethod("GET"); // 检查下载响应状态 int responseCode connection.getResponseCode(); if (responseCode HttpURLConnection.HTTPOK) { try (InputStream inputStream connection.getInputStream(); FileOutputStream outputStream new FileOutputStream(downloadPath)) { byte[] buffer new byte[4096]; int bytesRead; while ((bytesRead inputStream.read(buffer)) ! 1) { outputStream.write(buffer, 0, bytesRead); } System.out.println("文件下载成功。"); } } else { System.out.println("文件下载失败,状态码: " + responseCode); } }