Skip to content

Instantly share code, notes, and snippets.

@srbhbook
Last active April 26, 2022 17:39
Show Gist options
  • Save srbhbook/daadfc4791763ece4388ffd50646137c to your computer and use it in GitHub Desktop.
Save srbhbook/daadfc4791763ece4388ffd50646137c to your computer and use it in GitHub Desktop.
// get the jar files from github and include them as dependencies
importPackage(com.microsoft.azure.storage.*);
importPackage(com.microsoft.azure.storage.file.*);
// Configure the connection-string with your values
SERVER_NAME = "resource111111";
ACCOUNT_KEY = "verylonggibberishstring";
SHARE_NAME = "rootshare";//rootdirectory on your server
storageConnectionString =
"DefaultEndpointsProtocol=http;" +
"AccountName="+SERVER_NAME+";" +
"AccountKey="+ACCOUNT_KEY;
s1 = new com.microsoft.azure.storage.StorageCredentialsAccountAndKey(SERVER_NAME,ACCOUNT_KEY);
// Use the CloudStorageAccount object to connect to your storage account
try {
storageAccount = new com.microsoft.azure.storage.CloudStorageAccount(s1);
} catch (invalidKey) {
// Handle the exception
logger.info(invalidKey)
}
// Create the file storage client.
fileClient = storageAccount.createCloudFileClient();
// Get a reference to the file share
share = fileClient.getShareReference(SHARE_NAME);
//Get a reference to the root directory for the share.
rootDir = share.getRootDirectoryReference();
transfer = function(inpath,outpath){
mydir = rootDir.getDirectoryReference(inpath);
list1 = mydir.listFilesAndDirectories();
itr=list1.iterator();
filecount=0;
while(itr.hasNext()){
cloudfile=itr.next();
downloaded = 0;
try {
cloudfile.downloadToFile(outpath+cloudfile.getName());
downloaded=1;
} catch (e) {
logger.info("File not downloaded: " + cloudfile.getName() + " >> " + e);
}
if(downloaded==1){
//if downloaded, delete
deleted = 0;
attempt = 1;
while(deleted==0){
try{
cloudfile.deleteIfExists();
deleted = 1;
filecount++;
channelMap.put("filecount",filecount);
logger.info("File downloaded and deleted: " + cloudfile.getName() );
} catch (e2) {
logger.info("Attempt:" + attempt + "File not deleted: " + cloudfile.getName() + " >> " + e2);
}
}
}
}
}
inpath="sourcefolder/subfolder"; //give the folder name on azure
outpath="C:/Localfolder/"; // local folder. add extra / at end
transfer(inpath,outpath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment