簡體   English   中英

僅在while循環中調用函數時,才將文件發送到blob

[英]Sending file to blob only works when function is called in while loop

我有要修改的Azure blob示例代碼。 但是,uploadFile函數僅在開關情況下處於while循環中時才起作用。 如果我退出循環,它將創建容器,但無法上傳文件。

我嘗試將其取出並從代碼中的不同位置調用該函數,但是它們都不起作用。

uploadFile函數:

 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());
                });


    }

主要相關部分

            // 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情況下的uploadFile確實可以

由於上傳過程尚未完成且主過程已完成,因此不會上傳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);
        }
    }

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM