Professional Documents
Culture Documents
All rights
reserved.
// Licensed under the MIT License.
package com.azure.storage.file.share;
import com.azure.core.util.Configuration;
import com.azure.core.util.polling.SyncPoller;
import
com.azure.storage.file.share.models.ShareFileCopyInfo;
import
com.azure.storage.file.share.models.ShareFileProperties;
import
com.azure.storage.file.share.models.ShareStorageException;
import java.io.ByteArrayInputStream;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.nio.charset.StandardCharsets;
import java.nio.file.Files;
import java.time.Duration;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.TimeoutException;
/**
* Sample demonstrates how to create, copy and delete a
file and how to get and set properties.
*/
public class FileSample {
private static final String ENDPOINT =
Configuration.getGlobalConfiguration().get("PRIMARY_STORAGE_FILE_
/**
* The main method shows how to do the base operation
using file sync client.
*
* @param args No args needed for the main method.
* @throws RuntimeException If error occurs when make
storage API call.
*/
public static void main(String[] args) {
String shareName = generateRandomName();
ShareClient shareClient = new
ShareClientBuilder().connectionString(CONNECTION_STRING).endpoint
shareClient.create();
String parentDirName = generateRandomName();
shareClient.createDirectory(parentDirName);
.resourcePath(parentDirName + "/" +
srcFileName).buildFileClient();
.resourcePath(parentDirName + "/" +
destFileName).buildFileClient();
destFileClient.create(1024);
try {
:
poller.waitForCompletion(Duration.ofMinutes(15));
} catch (RuntimeException re) {
if (re.getCause() != null && re.getCause()
instanceof TimeoutException) {
try {
poller.cancelOperation();
} catch (ShareStorageException e) {
System.out.println("Failed to abort
the copy. Reasons: " + e.getMessage());
}
}
}
try {
srcFileClient.uploadFromFile(uploadPath);
} catch (ShareStorageException e) {
System.out.println("Failed to upload file to
storage. Reasons: " + e.getMessage());
}
if (Files.exists(downloadFile.toPath()) &&
!downloadFile.delete()) {
System.out.println("Failed to delete download
file.");
}