ImageResizer  3.4.0
 All Classes Namespaces Functions Variables Enumerations Enumerator Properties Events
Public Member Functions | Properties | List of all members
ImageResizer.Plugins.S3Reader.S3File Class Reference
Inheritance diagram for ImageResizer.Plugins.S3Reader.S3File:
Inheritance graph
[legend]
Collaboration diagram for ImageResizer.Plugins.S3Reader.S3File:
Collaboration graph
[legend]

Public Member Functions

void UpdateMetadata ()
 Updates the exists and modified date More...
 
 S3File (string virtualPath, S3VirtualPathProvider provider)
 
override Stream Open ()
 Returns an opened stream to the file contents. More...
 
string GetCacheKey (bool includeModifiedDate)
 

Properties

bool Exists [get]
 
DateTime ModifiedDateUTC [get]
 
- Properties inherited from ImageResizer.Plugins.IVirtualFile
string VirtualPath [get]
 The virtual path of the file (relative to the domain, like /app/folder/file.ext) More...
 
- Properties inherited from ImageResizer.Plugins.IVirtualFileWithModifiedDate
DateTime ModifiedDateUTC [get]
 The modified (last write time) of the source file, in UTC form. More...
 

Detailed Description

Definition at line 19 of file S3File.cs.

Member Function Documentation

override Stream ImageResizer.Plugins.S3Reader.S3File.Open ( )
inline

Returns an opened stream to the file contents.

Returns

Implements ImageResizer.Plugins.IVirtualFile.

Definition at line 105 of file S3File.cs.

105  {
106  //Synchronously download to memory stream
107  try {
108  var req = new Amazon.S3.Model.GetObjectRequest() { BucketName = bucket, Key = key };
109 
110  using (var s = provider.S3Client.GetObject(req)){
111  return StreamExtensions.CopyToMemoryStream(s.ResponseStream);
112  }
113  } catch (AmazonS3Exception se) {
114  if (se.StatusCode == System.Net.HttpStatusCode.NotFound || "NoSuchKey".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase)) throw new FileNotFoundException("Amazon S3 file not found", se);
115  else if ( se.StatusCode == System.Net.HttpStatusCode.Forbidden || "AccessDenied".Equals(se.ErrorCode, StringComparison.OrdinalIgnoreCase)) throw new FileNotFoundException("Amazon S3 access denied - file may not exist", se);
116  else throw;
117  }
118  return null;
119  }
void ImageResizer.Plugins.S3Reader.S3File.UpdateMetadata ( )
inline

Updates the exists and modified date

Definition at line 30 of file S3File.cs.

30  {
31  //Try to pull it out of the .net cache
32  string ckey = provider.VirtualFilesystemPrefix + "/" + bucket + "/" + key;
33  Cache c = HttpContext.Current.Cache;
34  object o = c.Get(ckey);
35  if (o is DateTime) {
36  _exists = true;
37  _fileModifiedDate = (Nullable<DateTime>)o;
38  } else if (o is bool) {
39  _exists = false;
40  _fileModifiedDate = null;
41  } else {
42  //Looks like we have to execute a head request
43  var request = new GetObjectMetadataRequest(){ BucketName = bucket, Key = key};
44 
45  try {
46  using (GetObjectMetadataResponse response = provider.S3Client.GetObjectMetadata(request)) {
47  //Exists
48  _exists = true;
49  _fileModifiedDate = response.LastModified;
50  }
51  } catch (AmazonS3Exception s3e) {
52  if (s3e.StatusCode == System.Net.HttpStatusCode.NotFound || s3e.StatusCode == System.Net.HttpStatusCode.Forbidden) {
53  //Doesn't exist
54  _exists = false;
55  _fileModifiedDate = null;
56  } else throw;
57  }
58  //Now, save to the .net cache
59  object obj = (_fileModifiedDate == null) ? (object)false : (object)_fileModifiedDate.Value;
60  //If MetadataAbsoluteExpiration is MaxValue, use DateTime.MaxValue.
61  c.Insert(ckey, obj, null, provider.MetadataAbsoluteExpiration == TimeSpan.MaxValue ? DateTime.MaxValue : DateTime.UtcNow.Add(provider.MetadataAbsoluteExpiration), provider.MetadataSlidingExpiration);
62 
63  }
64  }
TimeSpan MetadataAbsoluteExpiration
Existence and modified date metadata about files is cached for, at longest, this amount of time after...

The documentation for this class was generated from the following file: