?
Managing Azure file Sharing services using Java
?
The Azure File Sharing service provides a variety of ways to access interfaces, including Powershell,.net, Java, Python, and so on, and this chapter focuses on using Java to access Azure file storage.
?
- Java Basic Development Environment building, Eclipse plug-in installation, IntelliJ idea plug-in installation, please refer to my documentation:
http://cloudapps.blog.51cto.com/3136598/1772092
- With regard to the access connection string, the SDK default connection string is point to global azure, "*.core.windows.net", but the service URL of Azure in China is ". core.chinacloudapi.cn", Therefore, you need to specify Endpointsuffix in the link string.
?
- about the access protocol of the storage, by default is the HTTPS protocol, but you can also specify as the HTTP protocol, it is generally recommended to use HTTP when accessing storage inside azure, and use HTTPS for encrypted transmission when external access.
Public staticfinal String storageconnectionstring =
???????? "DEFAULTENDPOINTSPROTOCOL=HTTP;" +
???????? "Accountname=mystorageacctfile;" +
???????? "Accountkey=yourstorageaccountkey;" +
???????? "endpointsuffix=core.chinacloudapi.cn";
?
If you need to encrypt the transmission, modify the Defaultendpointsprotocol=https.
?
- FileShare names are required, for example, all lowercase must be Java inside you will see the following error :
Specific naming rules refer to: https://msdn.microsoft.com/library/azure/dn167011.aspx
- You first need to initialize the storage context to get a file access handle:
Storageaccount = Cloudstorageaccount. Parse(storageconnectionstring);
???????? System. Out . println (storageaccount. Getblobendpoint ());
???????? ?
???????? Cloudfileclient fileclient = storageaccount. createcloudfileclient ();
?
- To create a new file share:
Cloudfileshare Share = fileclient. Getsharereference (myfileshare);
????????
if (share. createifnotexists ())
{
System. out. println ("New File Share:" + myfileshare +"created!" );
}
?
- After the file share creation is complete, we create a directory under the file share:
Get a reference to the root directory for the share.
Cloudfiledirectory RootDir = share. getrootdirectoryreference ();
????????
Get a reference to the sampledir directory
Cloudfiledirectory Sampledir = rootdir. Getdirectoryreference (mydirectory);
?
if (sampledir. createifnotexists ())
{
System. out. println ("Sampledir created");
}
Else {
System. out. println ("Sampledir already exists");
}
- Uploading or downloading a file in a file share, the download file can be written to the local file through OutStream, and so on, this example directly print out:
Upload a test file to the sampledir
Cloudfile cloudfile = sampledir. Getfilereference ("hdinsight.publishsettings");
????????
if (! Cloudfile. Exists ())
{
???? cloudfile. Uploadfromfile (testfilepath);
}
Else
{
???? //download file if exists
???? System. Out . println (cloudfile. Downloadtext ());
}
- The following example shows how to delete a file, delete a directory, please note that when the directory is deleted, the directory must not have any files, or will be error:
?
Cloudfile cloudfile = sampledir. Getfilereference (testfilename);
???? //delete specified file
???? if ( cloudfile. deleteifexists ())
{
???? System. out. println (testfilename + "is deleted!" );
????????
}
?
Get a reference to the root directory for the share.
Cloudfiledirectory RootDir = share. getrootdirectoryreference ();
???????? ?
Get a reference to the sampledir directory
Cloudfiledirectory Sampledir = rootdir. Getdirectoryreference (mydirectory);
Delete the Directory
if ( sampledir. deleteifexists ())
{
???? System. out. println ("Directory"+ sampledir +"is deleted!" );
}
?
About when you call the Azure file interface, use the https link to link the Defaultendpointsprotocol is set to https, You may encounter the following error:
?
even if you are using the latest Azure China of the wosign of the certificate, the above problems can also occur, specific reasons and Azure China no matter, you understand:) The solution please refer to my blog:
?
http://cloudapps.blog.51cto.com/3136598/1744342
?
Azure file SMB3.0 files sharing Service (5)