public FileMetadata FileMetadata( string name ) { FileMetadata results = new FileMetadata(); FileInfo fi = new FileInfo( this.GetFullPath( name ) ); results.Exists = fi.Exists; if ( results.Exists ) { results.Length = fi.Length; results.LastModified = fi.LastWriteTimeUtc; } return results; }
public void Upload( string name, Stream content, FileMetadata FileMetadata ) { // FileMode.Create is "create or overwrite" http://www.csharp-examples.net/filestream-open-file/ using ( Stream fs = new FileStream( this.GetFullPath( name ), FileMode.Create, FileAccess.Write ) ) { content.CopyTo( fs ); fs.Flush(); } }
public void Upload( string name, Stream content, FileMetadata FileMetadata ) { Stream blobStream; long originalLength = content.Length; #if COMPRESSBLOBS // optionally put a compressor around the blob stream if ( this.ShouldCompressFile( name ) ) { // unfortunately, deflate stream doesn't allow seek, and we need a seekable stream // to pass to the blob storage stuff, so we compress into a memory stream MemoryStream compressedStream = new MemoryStream(); try { using ( DeflateStream compressor = new DeflateStream( compressedStream, CompressionMode.Compress, true ) ) { // compress to compressedOutputStream content.CopyTo( compressor ); } // seek back to beginning of comrpessed stream compressedStream.Seek( 0, SeekOrigin.Begin ); Debug.WriteLine( "COMPRESSED {0} -> {1} {2}% to {3}", originalLength, compressedStream.Length, ((float)compressedStream.Length / (float)originalLength) * 100, name ); } catch { // release the compressed stream resources if an error occurs compressedStream.Dispose(); throw; } blobStream = compressedStream; } else #endif { blobStream = content; } try { CloudBlockBlob _blob = this.blobContainer.GetBlockBlobReference( name ); // push the blobStream up to the cloud _blob.UploadFromStream( blobStream ); // set the metadata with the original index file properties _blob.Metadata["CachedLength"] = originalLength.ToString(); _blob.Metadata["CachedLastModified"] = FileMetadata.LastModified.ToString(); _blob.SetMetadata(); Debug.WriteLine( "PUT {1} bytes to {0} in cloud", name, blobStream.Length ); } finally { blobStream.Dispose(); } }
// Always returns an object even if file doesn't exist public FileMetadata FileMetadata( string name ) { FileMetadata results = new FileMetadata { Name = name }; try { CloudBlockBlob blob = this.blobContainer.GetBlockBlobReference( name ); blob.FetchAttributes(); results.Exists = true; // else it would've errored results.LastModified = blob.Properties.LastModified.Value.UtcDateTime; long blobLength; if ( long.TryParse( blob.Metadata["CachedLength"], out blobLength ) ) { results.Length = blobLength; } else { results.Length = blob.Properties.Length; // fall back to actual blob size } } catch ( Exception ) { results.Exists = false; } return results; }
// Always returns an object even if file doesn't exist public FileMetadata FileMetadata( string name ) { FileMetadata results = new FileMetadata { Name = name }; using ( AmazonS3 client = AWSClientFactory.CreateAmazonS3Client( this.amazonKey, this.amazonSecret ) ) { GetObjectMetadataRequest request = new GetObjectMetadataRequest { BucketName = this.amazonBucket, Key = name }; try { GetObjectMetadataResponse response = client.GetObjectMetadata( request ); results.Exists = true; // else AWSSDK threw results.Length = response.ContentLength; results.LastModified = response.LastModified; } catch ( AmazonS3Exception ex ) { if ( ex.ErrorCode == "NoSuchKey" ) { results.Exists = false; // File doesn't exist } else { throw; } } } return results; }
public void Upload( string name, Stream content, FileMetadata FileMetadata ) { using ( AmazonS3 client = AWSClientFactory.CreateAmazonS3Client( this.amazonKey, this.amazonSecret ) ) { PutObjectRequest request = new PutObjectRequest { BucketName = this.amazonBucket, Key = name, InputStream = content }; client.PutObject( request ); } }