2

I have a remote folder with n files and I need to copy content in another remote file. I guess it can be done through streams, and this is what I tried:

            WebRequest destRequest = WebRequest.Create(destFile);
            destRequest.Method = "PUT";
            destRequest.Headers.Add("x-ms-blob-type", "BlockBlob"); //just an example with Azure blob, doesn't matter


            using (Stream destStream = destRequest.GetRequestStream())
            {
                string sourceName = "mysourcefolder";

                int blockSize = 8388608; //all the files have the same lenght, except one (sometimes)
                for (int i = 0; i < n; i++)
                {
                    string source = sourceName + i;
                    WebRequest sourceRequest = WebRequest.Create(source);
                    destRequest.Method = "GET";
                    HttpWebResponse destResp = (HttpWebResponse)destRequest.GetResponse();
                    using (Stream sourceStream = destResp.GetResponseStream())
                    {
                        sourceStream.CopyTo(destStream, blockSize);
                    }
                }

                Console.Write("ok");
            }

        }
        catch (Exception e)
        {
            Console.Write("nope !");
        }

There are multiple issues in my code:

1) I have to specify the lenght in my PUT request. Probably it is blockSize*n since I have no exceptions about this;

2) If that is the case, I have still the exception Cannot close stream until all bytes are written. What does it means?

Fabrizio Morello
  • 335
  • 1
  • 5
  • 18

1 Answers1

1

There has been confusion in resource and dest requests. I have added comments to the changing lines.

        WebRequest destRequest = WebRequest.Create(destFile);
        destRequest.Method = "PUT";
        destRequest.Headers.Add("x-ms-blob-type", "BlockBlob"); //just an example with Azure blob, doesn't matter
        using (Stream destStream = destRequest.GetRequestStream())
        {
            string sourceName = "mysourcefolder";
            //int blockSize = 8388608; //all the files have the same lenght, except one (sometimes) //all the files have the same lenght, except one (sometimes)
            for (int i = 0; i < n; i++)
            {
                string source = sourceName + i;
                WebRequest sourceRequest = WebRequest.Create(source);
                destRequest.Method = "GET";

                //HttpWebResponse destResp = (HttpWebResponse)destRequest.GetResponse();
                //using (Stream sourceStream = destResp.GetResponseStream())

                // you need source response
                HttpWebResponse sourceResp = (HttpWebResponse)sourceRequest.GetResponse();
                using (Stream sourceStream = sourceResp.GetResponseStream())
                {
                    sourceStream.CopyTo(destStream);
                }
            }
            // The request is made here
            var destinationResponse = (HttpWebResponse) destRequest.GetResponse();
            //Console.Write("ok");
            Console.Write(destinationResponse.StatusCode.ToString());
        }
Fabrizio Morello
  • 335
  • 1
  • 5
  • 18
levent
  • 3,464
  • 1
  • 12
  • 22