|
| 1 | +package cn.ucloud.ufile.sample.object; |
| 2 | + |
| 3 | +import cn.ucloud.ufile.UfileClient; |
| 4 | +import cn.ucloud.ufile.api.object.ObjectConfig; |
| 5 | +import cn.ucloud.ufile.bean.ObjectProfile; |
| 6 | +import cn.ucloud.ufile.exception.UfileClientException; |
| 7 | +import cn.ucloud.ufile.exception.UfileServerException; |
| 8 | +import cn.ucloud.ufile.sample.Constants; |
| 9 | +import cn.ucloud.ufile.util.Etag; |
| 10 | +import cn.ucloud.ufile.util.JLog; |
| 11 | + |
| 12 | +import java.io.*; |
| 13 | + |
| 14 | +/** |
| 15 | + * @author: clark.liu |
| 16 | + |
| 17 | + * @date: 2021-02-23 |
| 18 | + */ |
| 19 | +public class ObjectETagSample { |
| 20 | + private static final String TAG = "GetObjectETagSample"; |
| 21 | + private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com"); |
| 22 | + |
| 23 | + public static void main(String[] args) { |
| 24 | + computeLocalFileETag(); |
| 25 | + // computeLocalFileETagUseStream(); |
| 26 | + // getBucketObjectETag(); |
| 27 | + // compareETag(); |
| 28 | + } |
| 29 | + |
| 30 | + public static void computeLocalFileETag() { |
| 31 | + String path = ""; |
| 32 | + try { |
| 33 | + File file = new File(path); |
| 34 | + Etag etag = Etag.etag(file); |
| 35 | + JLog.D(TAG, String.format("ETag is [%s]: file path [%s]", etag.geteTag(), path)); |
| 36 | + } catch (IOException e) { |
| 37 | + e.printStackTrace(); |
| 38 | + } |
| 39 | + } |
| 40 | + |
| 41 | + public static void computeLocalFileETagUseStream() { |
| 42 | + String path = ""; |
| 43 | + try { |
| 44 | + InputStream in = new FileInputStream(path); |
| 45 | + Etag etag = Etag.etag(in); |
| 46 | + JLog.D(TAG, String.format("ETag is [%s]: file path [%s]", etag.geteTag(), path)); |
| 47 | + } catch (IOException e) { |
| 48 | + e.printStackTrace(); |
| 49 | + } |
| 50 | + } |
| 51 | + |
| 52 | + public static void getBucketObjectETag() { |
| 53 | + String keyName = ""; |
| 54 | + String bucketName = ""; |
| 55 | + |
| 56 | + try { |
| 57 | + ObjectProfile objectProfile = UfileClient.object(Constants.OBJECT_AUTHORIZER, config) |
| 58 | + .objectProfile(keyName, bucketName).execute(); |
| 59 | + |
| 60 | + if (objectProfile == null) { |
| 61 | + JLog.D(TAG, String.format("key [%s] may be not exist", keyName)); |
| 62 | + return; |
| 63 | + } |
| 64 | + |
| 65 | + JLog.D(TAG, String.format("key [%s] ETag: %s", keyName, objectProfile.geteTag())); |
| 66 | + } catch (UfileClientException e) { |
| 67 | + e.printStackTrace(); |
| 68 | + } catch (UfileServerException e) { |
| 69 | + e.printStackTrace(); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + public static void compareETag() { |
| 74 | + String keyName = ""; |
| 75 | + String bucketName = ""; |
| 76 | + String localpath = ""; |
| 77 | + |
| 78 | + try { |
| 79 | + File f = new File(localpath); |
| 80 | + if (UfileClient.object(Constants.OBJECT_AUTHORIZER, config).compareEtag(f, keyName, bucketName)) { |
| 81 | + JLog.D(TAG, "ETag compare success"); |
| 82 | + } else { |
| 83 | + JLog.D(TAG, "ETag compare fail"); |
| 84 | + } |
| 85 | + } catch (UfileClientException e) { |
| 86 | + e.printStackTrace(); |
| 87 | + } catch (UfileServerException e) { |
| 88 | + e.printStackTrace(); |
| 89 | + } |
| 90 | + } |
| 91 | +} |
0 commit comments