Example #1
0
    static async ValueTask <bool> CopyLocalFileToStorageFile(StorageClient client, string localFilePath, string storagePath)
    {
        // TODO (pri 3): make file i/o more efficient
        FileInfo fileInfo = new FileInfo(localFilePath);

        var             createRequest = new CreateFileRequest(storagePath, fileInfo.Length);
        StorageResponse response      = await client.SendRequest(createRequest).ConfigureAwait(false);

        if (response.StatusCode == 201)
        {
            using (var bytes = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                var putRequest = new PutRangeRequest(storagePath, bytes);
                response = await client.SendRequest(putRequest).ConfigureAwait(false);

                if (response.StatusCode == 201)
                {
                    return(true);
                }
            }
        }

        Log.TraceEvent(TraceEventType.Error, 0, "Response Status Code {0}", response.StatusCode);
        return(false);
    }
Example #2
0
    static async ValueTask <bool> CopyLocalFileToStorageFile(StorageClient client, string localFilePath, string storagePath)
    {
        // TODO (pri 3): make file i/o more efficient
        FileInfo fileInfo = new FileInfo(localFilePath);

        var             createRequest = new CreateFileRequest(storagePath, fileInfo.Length);
        StorageResponse response      = await client.SendRequest(createRequest).ConfigureAwait(false);

        if (response.StatusCode == 201)
        {
            using (var bytes = new FileStream(localFilePath, FileMode.Open, FileAccess.Read, FileShare.Read, 4096, true))
            {
                long bytesLeft = bytes.Length;
                long index     = 0;
                int  length    = 1024 * 1024 * 4;
                while (bytesLeft > 0)
                {
                    if (bytesLeft < length)
                    {
                        length = (int)bytesLeft;
                    }
                    var putRequest = new PutRangeRequest(storagePath, bytes, index, length);
                    response = await client.SendRequest(putRequest).ConfigureAwait(false);

                    if (response.StatusCode != 201)
                    {
                        Log.TraceEvent(TraceEventType.Error, 0, "Response Status Code {0}", response.StatusCode);
                        return(false);
                    }
                    index     += length;
                    bytesLeft -= length;
                    Debug.Assert(bytesLeft >= 0);
                }
            }
        }

        return(true);
    }