c# - Amazon S3 Stream is too long -
when trying download large file, zip file size 2gb amazon s3, throws exception "stream long" . how reading file amazon stream
var s3file = new s3fileinfo(client, bucketname, objectkey); var stream = s3file.openread();
is possible read file contents small chunks , merge them locally?
-alan-
public class buketobjectresult { public bool success { get; set; } public long size { get; set; } } public void getbucketobjectdata() { try { buketobjectresult res = checkfile(); //chunk divide in small chunks eg : 2gb file : 20mb chunks int chunksize = (int)(res.size / chunk); if (!res.success && (res.size == 0 || chunksize <= 0)) { res.success = false; return ; } string filename = "your file name"; long startpostion = 0; long endposition = 0; while (startpostion >= 0) { byte[] chunk = new byte[chunksize]; endposition = chunksize + startpostion; if (endposition > res.size) //to rest of file endposition = res.size; getobjectrequest request = new getobjectrequest { bucketname = "your bucket name", key = "your key", byterange = new byterange(startpostion, endposition) }; using (getobjectresponse response = s3client.getobject(request)) using (stream responsestream = response.responsestream) using (filestream filestream = file.open(filename, filemode.append)) { int readindex = readchunk(responsestream, ref chunk); startpostion += readindex; if (readindex != 0) { filestream.write(chunk, 0, readindex); } if (readindex != chunk.length) // didn't read full chunk: we're done (read rest of bytes) break; } } // verify fileinfo fi = new fileinfo(filename); if (fi.length == res.size) { res.success = true; } } catch (exception e) { } } public buketobjectresult checkfile() { buketobjectresult res = new buketobjectresult() { success = false}; try { listobjectsrequest request = new listobjectsrequest() { bucketname = "bucketname here ", delimiter = "/", prefix = "location here" }; listobjectsresponse response = s3client.listobjects(request); if (response.s3objects != null && response.s3objects.count > 0) { s3object o = response.s3objects.where(x => x.size != 0).firstordefault(); if (o != null) { res.success = true; res.size = o.size; } } } catch (exception e) { } return res; }
Comments
Post a Comment