-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathBlockList Upload
57 lines (38 loc) · 2.31 KB
/
BlockList Upload
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
import com.microsoft.azure.storage.blob.*;
import com.microsoft.azure.storage.core.Base64;
import java.io.*;
import java.net.URISyntaxException;
import java.security.InvalidKeyException;
import java.util.ArrayList;
import java.util.List;
import com.microsoft.azure.storage.CloudStorageAccount;
import com.microsoft.azure.storage.StorageException;
public class testupload {
public static void main(String[] args) throws InvalidKeyException {
try {
CloudStorageAccount storageAccount = CloudStorageAccount.parse("storage-account connection string");
CloudBlobClient blobClient = storageAccount.createCloudBlobClient();
CloudBlobContainer container = blobClient.getContainerReference("test");
String fileKey = "testFile.txt";
CloudBlockBlob blockBlob = container.getBlockBlobReference(fileKey);
java.io.FileInputStream fis =
new java.io.FileInputStream("C:\\Users\\diverm\\Desktop\\testFile.txt");
String blockIdEncoded = Base64.encode(fileKey.getBytes());
blockBlob.uploadBlock(blockIdEncoded, fis, -1);
//List<BlockEntry> blockList = new ArrayList<BlockEntry>();
blockBlob.downloadBlockList();
if(!blockList.stream().anyMatch(blockEntry -> blockEntry.getId().equals(blockIdEncoded))) {
blockList.clear();
// there was only one block id hence we had to clear the blocklist and add it, else one can first compare the lists and append the new additions(in an iterative fashion).
blockList.add(new BlockEntry(blockIdEncoded));
}
blockBlob.commitBlockList(blockList);
} catch (URISyntaxException | StorageException e) {
e.printStackTrace();
} catch (FileNotFoundException e) {
e.printStackTrace();
} catch (IOException e) {
e.printStackTrace();
}
}
}