|
15 | 15 | import com.salesforce.multicloudj.common.exceptions.SubstrateSdkException; |
16 | 16 | import com.salesforce.multicloudj.common.exceptions.UnknownException; |
17 | 17 | import com.salesforce.multicloudj.common.provider.Provider; |
18 | | - |
19 | 18 | import java.util.stream.Collectors; |
20 | 19 |
|
21 | 20 | /** |
22 | | - * An implementation of the {@link AbstractBlobClient} for Ali OSS |
23 | | - * AliBlobClient is service client for interacting with Alibaba Cloud Blob Storage. |
| 21 | + * An implementation of the {@link AbstractBlobClient} for Ali OSS AliBlobClient is service client |
| 22 | + * for interacting with Alibaba Cloud Blob Storage. |
24 | 23 | * |
25 | | - * <p>This class provides methods to access Ali resources using Alibaba OSS JDK to interact |
26 | | - * with the OSS service. |
| 24 | + * <p>This class provides methods to access Ali resources using Alibaba OSS JDK to interact with the |
| 25 | + * OSS service. |
27 | 26 | */ |
28 | 27 | public class AliBlobClient extends AbstractBlobClient<AliBlobClient> { |
29 | | - private OSS ossClient; |
| 28 | + private OSS ossClient; |
30 | 29 |
|
31 | | - /** |
32 | | - * Constructs an {@link AliBlobClient} using the provided builder. |
33 | | - * |
34 | | - * @param builder the builder to use to construct the AliBlobClient |
35 | | - */ |
36 | | - protected AliBlobClient(Builder builder) { |
37 | | - super(builder); |
| 30 | + /** |
| 31 | + * Constructs an {@link AliBlobClient} using the provided builder. |
| 32 | + * |
| 33 | + * @param builder the builder to use to construct the AliBlobClient |
| 34 | + */ |
| 35 | + protected AliBlobClient(Builder builder) { |
| 36 | + super(builder); |
38 | 37 |
|
39 | | - String endpoint = "https://oss-" + region + ".aliyuncs.com"; |
40 | | - if (builder.getEndpoint() != null) { |
41 | | - endpoint = builder.getEndpoint().getHost(); |
42 | | - } |
43 | | - OSSClientBuilder.OSSClientBuilderImpl ossClientBuilderImpl = OSSClientBuilder.create().endpoint(endpoint); |
44 | | - ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); |
45 | | - clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); |
46 | | - if (builder.getProxyEndpoint() != null) { |
47 | | - clientBuilderConfiguration.setProxyHost(builder.getProxyEndpoint().getHost()); |
48 | | - clientBuilderConfiguration.setProxyPort(builder.getProxyEndpoint().getPort()); |
49 | | - } |
50 | | - CredentialsProvider credentialsProvider = OSSCredentialsProvider.getCredentialsProvider(credentialsOverrider, region); |
51 | | - if (credentialsProvider != null) { |
52 | | - ossClientBuilderImpl.credentialsProvider(credentialsProvider); |
53 | | - } |
54 | | - ossClientBuilderImpl.clientConfiguration(clientBuilderConfiguration); |
55 | | - ossClient = ossClientBuilderImpl.build(); |
| 38 | + String endpoint = "https://oss-" + region + ".aliyuncs.com"; |
| 39 | + if (builder.getEndpoint() != null) { |
| 40 | + endpoint = builder.getEndpoint().getHost(); |
56 | 41 | } |
57 | | - |
58 | | - /** |
59 | | - * Lists all buckets in the Alibaba Cloud Blob Storage account associated with this client. |
60 | | - * |
61 | | - * @return a {@link ListBucketsResponse} containing a list of all buckets int the current region for this account |
62 | | - */ |
63 | | - @Override |
64 | | - protected ListBucketsResponse doListBuckets() { |
65 | | - return ListBucketsResponse.builder().bucketInfoList(ossClient.listBuckets().stream().map(bucket -> BucketInfo.builder() |
66 | | - .name(bucket.getName()) |
67 | | - .region(bucket.getRegion()) |
68 | | - .creationDate(bucket.getCreationDate().toInstant()) |
69 | | - .build()).collect(Collectors.toList())).build(); |
| 42 | + OSSClientBuilder.OSSClientBuilderImpl ossClientBuilderImpl = |
| 43 | + OSSClientBuilder.create().endpoint(endpoint); |
| 44 | + ClientBuilderConfiguration clientBuilderConfiguration = new ClientBuilderConfiguration(); |
| 45 | + clientBuilderConfiguration.setSignatureVersion(SignVersion.V4); |
| 46 | + if (builder.getProxyEndpoint() != null) { |
| 47 | + clientBuilderConfiguration.setProxyHost(builder.getProxyEndpoint().getHost()); |
| 48 | + clientBuilderConfiguration.setProxyPort(builder.getProxyEndpoint().getPort()); |
70 | 49 | } |
71 | | - |
72 | | - /** |
73 | | - * Creates a new bucket with the specified name. |
74 | | - * |
75 | | - * @param bucketName The name of the bucket to create |
76 | | - */ |
77 | | - @Override |
78 | | - protected void doCreateBucket(String bucketName) { |
79 | | - ossClient.createBucket(bucketName); |
| 50 | + CredentialsProvider credentialsProvider = |
| 51 | + OSSCredentialsProvider.getCredentialsProvider(credentialsOverrider, region); |
| 52 | + if (credentialsProvider != null) { |
| 53 | + ossClientBuilderImpl.credentialsProvider(credentialsProvider); |
80 | 54 | } |
| 55 | + ossClientBuilderImpl.clientConfiguration(clientBuilderConfiguration); |
| 56 | + ossClient = ossClientBuilderImpl.build(); |
| 57 | + } |
81 | 58 |
|
82 | | - /** |
83 | | - * Returns a {@link Provider.Builder} for creating a Provider for this class. |
84 | | - * |
85 | | - * @return a {@link Provider.Builder} for creating a Provider for this class |
86 | | - */ |
87 | | - @Override |
88 | | - public Provider.Builder builder() { |
89 | | - return new AliBlobStore.Builder(); |
90 | | - } |
| 59 | + /** |
| 60 | + * Lists all buckets in the Alibaba Cloud Blob Storage account associated with this client. |
| 61 | + * |
| 62 | + * @return a {@link ListBucketsResponse} containing a list of all buckets int the current region |
| 63 | + * for this account |
| 64 | + */ |
| 65 | + @Override |
| 66 | + protected ListBucketsResponse doListBuckets() { |
| 67 | + return ListBucketsResponse.builder() |
| 68 | + .bucketInfoList( |
| 69 | + ossClient.listBuckets().stream() |
| 70 | + .map( |
| 71 | + bucket -> |
| 72 | + BucketInfo.builder() |
| 73 | + .name(bucket.getName()) |
| 74 | + .region(bucket.getRegion()) |
| 75 | + .creationDate(bucket.getCreationDate().toInstant()) |
| 76 | + .build()) |
| 77 | + .collect(Collectors.toList())) |
| 78 | + .build(); |
| 79 | + } |
91 | 80 |
|
92 | | - /** |
93 | | - * Returns the appropriate SubstrateSdkException subclass for the given Throwable. |
94 | | - */ |
95 | | - @Override |
96 | | - public Class<? extends SubstrateSdkException> getException(Throwable t) { |
97 | | - if (t instanceof SubstrateSdkException) { |
98 | | - return (Class<? extends SubstrateSdkException>) t.getClass(); |
99 | | - } else if (t instanceof ServiceException) { |
100 | | - String errorCode = ((ServiceException) t).getErrorCode(); |
101 | | - return ErrorCodeMapping.getException(errorCode); |
102 | | - } else if (t instanceof ClientException) { |
103 | | - return InvalidArgumentException.class; |
104 | | - } |
105 | | - return UnknownException.class; |
| 81 | + /** |
| 82 | + * Creates a new bucket with the specified name. |
| 83 | + * |
| 84 | + * @param bucketName The name of the bucket to create |
| 85 | + */ |
| 86 | + @Override |
| 87 | + protected void doCreateBucket(String bucketName) { |
| 88 | + ossClient.createBucket(bucketName); |
| 89 | + } |
| 90 | + |
| 91 | + /** |
| 92 | + * Returns a {@link Provider.Builder} for creating a Provider for this class. |
| 93 | + * |
| 94 | + * @return a {@link Provider.Builder} for creating a Provider for this class |
| 95 | + */ |
| 96 | + @Override |
| 97 | + public Provider.Builder builder() { |
| 98 | + return new AliBlobStore.Builder(); |
| 99 | + } |
| 100 | + |
| 101 | + /** Returns the appropriate SubstrateSdkException subclass for the given Throwable. */ |
| 102 | + @Override |
| 103 | + public Class<? extends SubstrateSdkException> getException(Throwable t) { |
| 104 | + if (t instanceof SubstrateSdkException) { |
| 105 | + return (Class<? extends SubstrateSdkException>) t.getClass(); |
| 106 | + } else if (t instanceof ServiceException) { |
| 107 | + String errorCode = ((ServiceException) t).getErrorCode(); |
| 108 | + return ErrorCodeMapping.getException(errorCode); |
| 109 | + } else if (t instanceof ClientException) { |
| 110 | + return InvalidArgumentException.class; |
106 | 111 | } |
| 112 | + return UnknownException.class; |
| 113 | + } |
107 | 114 |
|
108 | | - /** |
109 | | - * Closes the underlying OSS client and releases any resources. |
110 | | - */ |
111 | | - @Override |
112 | | - public void close() { |
113 | | - if (ossClient != null) { |
114 | | - ossClient.shutdown(); |
115 | | - } |
| 115 | + /** Closes the underlying OSS client and releases any resources. */ |
| 116 | + @Override |
| 117 | + public void close() { |
| 118 | + if (ossClient != null) { |
| 119 | + ossClient.shutdown(); |
116 | 120 | } |
| 121 | + } |
117 | 122 |
|
118 | | - public static class Builder extends AbstractBlobClient.Builder<AliBlobClient> { |
| 123 | + public static class Builder extends AbstractBlobClient.Builder<AliBlobClient> { |
119 | 124 |
|
120 | | - public Builder() { |
121 | | - providerId(AliConstants.PROVIDER_ID); |
122 | | - } |
| 125 | + public Builder() { |
| 126 | + providerId(AliConstants.PROVIDER_ID); |
| 127 | + } |
123 | 128 |
|
124 | | - @Override |
125 | | - public AliBlobClient build() { |
126 | | - return new AliBlobClient(this); |
127 | | - } |
| 129 | + @Override |
| 130 | + public AliBlobClient build() { |
| 131 | + return new AliBlobClient(this); |
128 | 132 | } |
| 133 | + } |
129 | 134 | } |
0 commit comments