比较本地文件和远程文件etag

SDK 提供CompareFileEtag方法比较本地文件和远程文件etag,适用于文件完整性判断,例如文件上传、下载过程中是否发生丢失。

示例如下:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
public class ObjectETagSample {
    private static final String TAG = "GetObjectETagSample";
    private static ObjectConfig config = new ObjectConfig("cn-sh2", "ufileos.com");

    public static void main(String[] args) {
        computeLocalFileETag();
        // computeLocalFileETagUseStream();
        // getBucketObjectETag();
        // compareETag();
    }

    public static void computeLocalFileETag() {
        String path = "";
        try {
            File file = new File(path);
            Etag etag = Etag.etag(file);
            JLog.D(TAG, String.format("ETag is [%s]: file path [%s]", etag.geteTag(), path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void computeLocalFileETagUseStream() {
        String path = "";
        try {
            InputStream in = new FileInputStream(path);
            Etag etag = Etag.etag(in);
            JLog.D(TAG, String.format("ETag is [%s]: file path [%s]", etag.geteTag(), path));
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    public static void getBucketObjectETag() {
        String keyName = "";
        String bucketName = "";

        try {
            ObjectProfile objectProfile = UfileClient.object(Constants.OBJECT_AUTHORIZER, config)
                    .objectProfile(keyName, bucketName).execute();

            if (objectProfile == null) {
                JLog.D(TAG, String.format("key [%s] may be not exist", keyName));
                return;
            }

            JLog.D(TAG, String.format("key [%s] ETag: %s", keyName, objectProfile.geteTag()));
        } catch (UfileClientException e) {
            e.printStackTrace();
        } catch (UfileServerException e) {
            e.printStackTrace();
        }
    }

    public static void compareETag() {
        String keyName = "";
        String bucketName = "";
        String localpath = "";

        try {
            File f = new File(localpath);
            if (UfileClient.object(Constants.OBJECT_AUTHORIZER, config).compareEtag(f, keyName, bucketName)) {
                JLog.D(TAG, "ETag compare success");
            } else {
                JLog.D(TAG, "ETag compare fail");
            }
        } catch (UfileClientException e) {
            e.printStackTrace();
        } catch (UfileServerException e) {
            e.printStackTrace();
        }
    }
}