您好,登錄后才能下訂單哦!
這篇文章主要講解了“SpringBoot整合aws的方法是什么”,文中的講解內容簡單清晰,易于學習與理解,下面請大家跟著小編的思路慢慢深入,一起來研究和學習“SpringBoot整合aws的方法是什么”吧!
<dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>s3</artifactId> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-s3</artifactId> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-sqs</artifactId> </dependency> <dependency> <groupId>software.amazon.awssdk</groupId> <artifactId>sns</artifactId> </dependency> <dependency> <groupId>com.amazonaws</groupId> <artifactId>aws-java-sdk-cloudfront</artifactId> </dependency>
private S3Client createClient() { AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey)); S3Client s3 = S3Client.builder() .region(Region.CN_NORTHWEST_1) .credentialsProvider(credentialsProvider) .endpointOverride(URI.create(minIoUrl)) .build(); return s3; }
public class MinioOperate { private String minIoAccessKey; private String minIoSecretKey; private String minIoUrl; public MinioOperate() { } public MinioOperate(String minIoAccessKey, String minIoSecretKey, String minIoUrl) { this.minIoAccessKey = minIoAccessKey; this.minIoSecretKey = minIoSecretKey; this.minIoUrl = minIoUrl; } #創建aws的客戶端 private S3Client createClient() { AwsCredentialsProvider credentialsProvider = StaticCredentialsProvider.create(AwsBasicCredentials.create(minIoAccessKey, minIoSecretKey)); S3Client s3 = S3Client.builder() .region(Region.CN_NORTHWEST_1) .credentialsProvider(credentialsProvider) .endpointOverride(URI.create(minIoUrl)) .build(); return s3; } // public String generatePresignedUrl(String bucketName, String objectKey, String acl) { // URL url = null; // try { // AWSStaticCredentialsProvider credentialsProvider = // new AWSStaticCredentialsProvider( // new BasicAWSCredentials(minIoAccessKey, minIoSecretKey)); // // AmazonS3ClientBuilder builder = AmazonS3ClientBuilder.standard(); // builder.withEndpointConfiguration(new AwsClientBuilder.EndpointConfiguration(minIoUrl, Regions.CN_NORTHWEST_1.getName())); // // AmazonS3 s3Client = builder // .withPathStyleAccessEnabled(true) // .withCredentials(credentialsProvider) // .build(); // // // Set the presigned URL to expire after one hour. // Date expiration = new Date(); // long expTimeMillis = expiration.getTime(); // expTimeMillis += 1000 * 60 * 60 * 4; // expiration.setTime(expTimeMillis); // // // Generate the presigned URL. // GeneratePresignedUrlRequest generatePresignedUrlRequest = // new GeneratePresignedUrlRequest(bucketName, objectKey) // .withMethod(HttpMethod.GET) // .withExpiration(expiration); // // // set acl // if (!StringUtils.isEmpty(acl)) { // generatePresignedUrlRequest.withMethod(HttpMethod.PUT); // generatePresignedUrlRequest.addRequestParameter(Headers.S3_CANNED_ACL, acl); // } // // url = s3Client.generatePresignedUrl(generatePresignedUrlRequest); // // } catch (AmazonServiceException e) { // // The call was transmitted successfully, but Amazon S3 couldn't process // // it, so it returned an error response. // e.printStackTrace(); // } catch (SdkClientException e) { // // Amazon S3 couldn't be contacted for a response, or the client // // couldn't parse the response from Amazon S3. // e.printStackTrace(); // } // // if (StringUtils.isEmpty(url)) { // return null; // } // return url.toString(); // } #獲取所有的對象(根據桶和前綴) public List<S3Object> ListObjects(String bucket, String prefix) { S3Client s3Client = this.createClient(); List<S3Object> contents = null; try { ListObjectsV2Request request = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix).build(); ListObjectsV2Response listObjectsV2Response = s3Client.listObjectsV2(request); contents = listObjectsV2Response.contents(); } finally { this.closeClient(s3Client); } return contents; } #上傳對象 public void putObject(String bucket, String key, String content) { S3Client s3Client = this.createClient(); try { ByteBuffer byteBuffer = ByteBuffer.wrap(content.getBytes(StandardCharsets.UTF_8)); PutObjectResponse putObjectResponse = s3.putObject(PutObjectRequest.builder().bucket(bucket).key(key) .build(), RequestBody.fromByteBuffer(byteBuffer)); } finally { this.closeClient(s3); } } #上傳文件 public void putFile(String filePath, String key, String bucket) { S3Client s3Client = this.createClient(); File tempFile = new File(filePath); try { PutObjectResponse putObjectResponse = s3Client.putObject(PutObjectRequest.builder().bucket(bucket).key(key) .build(), RequestBody.fromFile(tempFile)); } catch (Exception e) { e.printStackTrace(); } finally { this.closeClient(s3Client); } } #獲取對象大小 @Override public long getS3ObjectLength(String bucket, String key) { S3Client s3Client = this.createClient(); long size = 0; try { List<S3Object> s3Objects = this.ListObjects(s3, bucket, key); size = s3Objects.get(0).size(); }catch (Exception e){ e.printStackTrace(); }finally { this.closeClient(s3); } return size; } # 獲取對象 public String getObject(String bucket, String key) { S3Client s3Client = this.createClient(); try { ResponseBytes<GetObjectResponse> responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toBytes()); # ResponseTransformer.toBytes() 將響應轉換為二進制流 return this.decode(responseResponseBytes.asByteBuffer()); } finally { this.closeClient(s3); } } # 獲取對象的流 @Override public InputStream getInputStream(String bucket, String key) { S3Client s3Client = this.createClient(); try { ResponseBytes<GetObjectResponse> responseResponseBytes = s3.getObject(GetObjectRequest.builder().bucket(bucket).key(key).build(), ResponseTransformer.toBytes()); return responseResponseBytes.asInputStream(); } finally { this.closeClient(s3); } } #刪除對象 public void deleteObject(String bucket, String key) { S3Client s3Client = this.createClient(); try { DeleteObjectRequest deleteObjectRequest = DeleteObjectRequest.builder().bucket(bucket).key(key).build(); DeleteObjectResponse deleteObjectResponse = s3.deleteObject(deleteObjectRequest); } catch (Exception e) { e.printStackTrace(); } finally { this.closeClient(s3); } } #批量刪除對象 public void deleteObjects(List<String> buckets, String key) { S3Client s3Client = this.createClient(); try { String prefix = key.substring(0, key.lastIndexOf(File.separator)); for (String bucket : buckets) { ListObjectsRequest listObjectsRequest = ListObjectsRequest.builder().bucket(bucket).prefix(prefix).build(); ListObjectsResponse listObjectsResponse = s3.listObjects(listObjectsRequest); List<S3Object> contents = listObjectsResponse.contents(); for (S3Object content : contents) { String objectKey = content.key(); this.deleteObject(s3, bucket, objectKey); } this.deleteObject(s3, bucket, prefix); } } finally { this.closeClient(s3); } } } public String decode(ByteBuffer byteBuffer) { Charset charset = StandardCharsets.UTF_8; return charset.decode(byteBuffer).toString(); }
其中 minIoAccessKey,minIoSecretKey, minIoUrl;分別對應賬號、密碼、請求地址。需要對應自己的相關信息。
感謝各位的閱讀,以上就是“SpringBoot整合aws的方法是什么”的內容了,經過本文的學習后,相信大家對SpringBoot整合aws的方法是什么這一問題有了更深刻的體會,具體使用情況還需要大家實踐驗證。這里是億速云,小編將為大家推送更多相關知識點的文章,歡迎關注!
免責聲明:本站發布的內容(圖片、視頻和文字)以原創、轉載和分享為主,文章觀點不代表本網站立場,如果涉及侵權請聯系站長郵箱:is@yisu.com進行舉報,并提供相關證據,一經查實,將立刻刪除涉嫌侵權內容。