| | |
| | | |
| | | import com.aliyun.oss.OSS; |
| | | import com.aliyun.oss.OSSClientBuilder; |
| | | import com.aliyun.oss.OSSException; |
| | | import com.aliyun.oss.model.OSSObject; |
| | | import com.aliyuncs.DefaultAcsClient; |
| | | import com.aliyuncs.IAcsClient; |
| | |
| | | } |
| | | OSS ossClient = this.createClient(); |
| | | // First check if the object can be fetched. |
| | | ossClient.getObject(bucket, objectKey); |
| | | boolean isExist = ossClient.doesObjectExist(bucket, objectKey); |
| | | if (!isExist) { |
| | | throw new OSSException("The object does not exist."); |
| | | } |
| | | |
| | | return ossClient.generatePresignedUrl(bucket, objectKey, |
| | | new Date(System.currentTimeMillis() + configuration.getExpire() * 1000)); |
| | |
| | | @Override |
| | | public Boolean deleteObject(String bucket, String objectKey) { |
| | | OSS ossClient = this.createClient(); |
| | | if (!ossClient.doesObjectExist(bucket, objectKey)) { |
| | | ossClient.shutdown(); |
| | | return true; |
| | | } |
| | | ossClient.deleteObject(bucket, objectKey); |
| | | ossClient.shutdown(); |
| | | return true; |
| | |
| | | public byte[] getObject(String bucket, String objectKey) { |
| | | OSS ossClient = this.createClient(); |
| | | OSSObject object = ossClient.getObject(bucket, objectKey); |
| | | InputStream stream = object.getObjectContent(); |
| | | |
| | | try { |
| | | try (InputStream stream = object.getObjectContent()) { |
| | | return stream.readAllBytes(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } finally { |
| | | try { |
| | | stream.close(); |
| | | ossClient.shutdown(); |
| | | } catch (IOException e) { |
| | | e.printStackTrace(); |
| | | } |
| | | ossClient.shutdown(); |
| | | } |
| | | return new byte[0]; |
| | | } |