仅当在 while 循环中调用函数时,将文件发送到 blob 才有效
Posted
技术标签:
【中文标题】仅当在 while 循环中调用函数时,将文件发送到 blob 才有效【英文标题】:Sending file to blob only works when function is called in while loop 【发布时间】:2019-11-22 11:18:22 【问题描述】:我有正在尝试修改的 Azure blob 示例代码。但是,uploadFile 函数仅在 switch 情况下的 while 循环中有效。如果我将它从循环中取出,它会创建容器但无法上传文件。
我尝试将其取出并从代码中的不同位置调用该函数,但它们都不起作用。
上传文件功能:
static void uploadFile(BlockBlobURL blob, File sourceFile) throws IOException
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(sourceFile.toPath());
// Uploading a file to the blobURL using the high-level methods available in TransferManager class
// Alternatively call the PutBlob/PutBlock low-level methods from BlockBlobURL type
TransferManager.uploadFileToBlockBlob(fileChannel, blob, 8*1024*1024, null, null)
.subscribe(response->
System.out.println("Completed upload request.");
TimeUnit.SECONDS.sleep(5);
System.out.println(response.response().statusCode());
);
main的相关部分
// Listening for commands from the console
//THIS IS THE PART THAT ONLY MAKES THE CONTAINER
/*
System.out.println("Uploading the sample file into the container: " + containerURL );
uploadFile(blobURL, sampleFile);
System.out.println("File Uploaded");
*/
//TRYING TO CALL FUNCTION FROM OUTSIDE WHILE, BUT IT ONLY WORKS HERE
System.out.println("Enter a command");
System.out.println("(P)utBlob | (L)istBlobs | (G)etBlob");
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
while (true)
System.out.println("# Enter a command : ");
String input = reader.readLine();
switch(input)
case "P":
System.out.println("Uploading the sample file into the container: " + containerURL );
uploadFile(blobURL, sampleFile);
break;
在 while 循环之外调用的 uploadFile 会创建容器,但实际上不会将文件上传到 blob,而在 while 循环和 switch case 中调用的 uploadFile 会
【问题讨论】:
是的,现在可以使用了。感谢您的帮助 【参考方案1】:这不会上传 blob,因为上传过程尚未完成且主过程已完成。所以我只是在主进程中添加一个Thread.sleep()
。并且提醒不要将值设置得太短,否则仍然会失败。在我的测试中,我将其设置为 2000
毫秒。
public static void main(String[] args) throws java.lang.Exception
ContainerURL containerURL;
// Creating a sample file to use in the sample
File sampleFile = null;
try
sampleFile = File.createTempFile("downloadedFile", ".txt");
// Retrieve the credentials and initialize SharedKeyCredentials
String accountName = "xxxxxx";
String accountKey = "xxxxxxx";
// Create a ServiceURL to call the Blob service. We will also use this to construct the ContainerURL
SharedKeyCredentials creds = new SharedKeyCredentials(accountName, accountKey);
// We are using a default pipeline here, you can learn more about it at https://github.com/Azure/azure-storage-java/wiki/Azure-Storage-Java-V10-Overview
final ServiceURL serviceURL = new ServiceURL(new URL("https://" + accountName + ".blob.core.windows.net"), StorageURL.createPipeline(creds, new PipelineOptions()));
// Let's create a container using a blocking call to Azure Storage
// If container exists, we'll catch and continue
containerURL = serviceURL.createContainerURL("quickstart");
try
ContainerCreateResponse response = containerURL.create(null, null, null).blockingGet();
System.out.println("Container Create Response was " + response.statusCode());
catch (RestException e)
if (e instanceof RestException && ((RestException)e).response().statusCode() != 409)
throw e;
else
System.out.println("quickstart container already exists, resuming...");
// Create a BlockBlobURL to run operations on Blobs
final BlockBlobURL blobURL = containerURL.createBlockBlobURL("SampleBlob.txt");
System.out.println("Uploading the sample file into the container: " + containerURL );
AsynchronousFileChannel fileChannel = AsynchronousFileChannel.open(sampleFile.toPath());
// Uploading a file to the blobURL using the high-level methods available in TransferManager class
// Alternatively call the PutBlob/PutBlock low-level methods from BlockBlobURL type
TransferManager.uploadFileToBlockBlob(fileChannel, blobURL, 8*1024*1024, null, null)
.subscribe(response->
System.out.println("Completed upload request.");
System.out.println(response.response().statusCode());
);
Thread.sleep(2000);
catch (InvalidKeyException e)
System.out.println("Invalid Storage account name/key provided");
catch (MalformedURLException e)
System.out.println("Invalid URI provided");
catch (RestException e)
System.out.println("Service error returned: " + e.response().statusCode() );
catch (IOException e)
e.printStackTrace();
System.exit(-1);
【讨论】:
以上是关于仅当在 while 循环中调用函数时,将文件发送到 blob 才有效的主要内容,如果未能解决你的问题,请参考以下文章
仅当在流程中添加帐户时,Google 登录才会将显示名称返回为 null
Folders.hasNext()从Google表格中返回false,但直接从Google Script调用时为true
是否有任何函数可以将c ++编程中的流转移到while循环或while循环中?