Exemplo n.º 1
0
 static void Test()
 {
     UploadFile(@"D:\Research\KB\kb.entity.subset");
     DownLoadFile("kb.entity.subset");
     var wclient = new NameNodeServiceClient();
     wclient.DeleteFile("kb.entity.subset");
     FileServiceClient client = new FileServiceClient("BasicHttpBinding_IFileService", "http://*****:*****@"D:\Research\KB\kb.entity", FileMode.Open);
     var stream = new CombinedStream(streams);
     client.UploadChunk(stream);
 }
Exemplo n.º 2
0
        static void UploadFile(string filePath)
        {
            var fileInfo = new FileInfo(filePath);
            NameNodeServiceClient client = new NameNodeServiceClient();
            var uploadInfo = client.GetUploadInfo(fileInfo.Name, fileInfo.Length);

            int numOfChunks = uploadInfo.Chunks.Select(x => x.Order).Distinct().Count();
            int bufferSize = 1024 * 8 * 64;
            using (FileStream reader = new FileStream(filePath, FileMode.Open))
            {
                long byteNumPerChunk = reader.Length / numOfChunks;
                for (int i = 0; i < numOfChunks; i++)
                {
                    var bytes = new byte[bufferSize];
                    long end = i == numOfChunks - 1 ? reader.Length : byteNumPerChunk * (i + 1);
                    int curBufferSize = byteNumPerChunk > (long)bufferSize ? bufferSize : (int)byteNumPerChunk;

                    string tempFileName = filePath + "_" + i;

                    using (FileStream writer = new FileStream(tempFileName, FileMode.Create))
                    {
                        int size;
                        while (reader.Position < end && (size = reader.Read(bytes, 0, curBufferSize)) > 0)
                        {
                            writer.Write(bytes, 0, size);
                            if (end - reader.Position < (long)bufferSize)
                            {
                                curBufferSize = (int)(end - reader.Position);
                            }
                        }
                    }

                    var chunks = uploadInfo.Chunks.Where(x => x.Order == i);
                    foreach(var chunk in chunks)
                    {
                        var streams = new Stream[2];
                        streams[0] = new MemoryStream(Guid.Parse(chunk.GUID).ToByteArray());
                        streams[1] = new FileStream(tempFileName, FileMode.Open);
                        var uploadStream = new CombinedStream(streams);
                        FileServiceClient serverClient = new FileServiceClient("BasicHttpBinding_IFileService", chunk.FileServer.FileServerServicePoint);
                        serverClient.UploadChunk(uploadStream);
                        streams[0].Close();
                        streams[1].Close();
                    }
                    File.Delete(tempFileName);
                }
            }
        }