Windows Azure: create a file in a cloud block container

I am writing a program that will run in the cloud. The program will generate output that should be written to a file and the file should be saved in a blob container.

I have no idea how to do this

Will this code

FileStream fs = new FileStream(file, FileMode.Create);
        StreamWriter sw = new StreamWriter(fs);

      

generate a file named "file" in the cloud ...


Oh .. how to store the content in a blob ..

+2


source to share


1 answer


Are you trying to load a page frame or block block? Usually block blobs are what you need, unless you are going to create a virtual machine from a blob image, then a page frame is needed.

Something like this works. This snippet is taken from the most excellent blob transfer utility . Check it out for all your download and upload needs. (Just change the type from block to page if you need VHD)



public void UploadBlobAsync(ICloudBlob blob, string LocalFile)
    {
        // The class currently stores state in class level variables so calling UploadBlobAsync or DownloadBlobAsync a second time will cause problems.
        // A better long term solution would be to better encapsulate the state, but the current solution works for the needs of my primary client.
        // Throw an exception if UploadBlobAsync or DownloadBlobAsync has already been called.
        lock (WorkingLock)
        {
            if (!Working)
                Working = true;
            else
                throw new Exception("BlobTransfer already initiated. Create new BlobTransfer object to initiate a new file transfer.");
        }

        // Attempt to open the file first so that we throw an exception before getting into the async work
        using (FileStream fstemp = new FileStream(LocalFile, FileMode.Open, FileAccess.Read)) { }

        // Create an async op in order to raise the events back to the client on the correct thread.
        asyncOp = AsyncOperationManager.CreateOperation(blob);

        TransferType = TransferTypeEnum.Upload;
        m_Blob = blob;
        m_FileName = LocalFile;

        var file = new FileInfo(m_FileName);
        long fileSize = file.Length;

        FileStream fs = new FileStream(m_FileName, FileMode.Open, FileAccess.Read, FileShare.Read);
        ProgressStream pstream = new ProgressStream(fs);
        pstream.ProgressChanged += pstream_ProgressChanged;
        pstream.SetLength(fileSize);
        m_Blob.ServiceClient.ParallelOperationThreadCount = 10;
        asyncresult = m_Blob.BeginUploadFromStream(pstream, BlobTransferCompletedCallback, new BlobTransferAsyncState(m_Blob, pstream));
    }

      

+1


source







All Articles