This is a migrated thread and some comments may be shown as answers.

S3 set expires header

4 Answers 33 Views
CloudUpload
This is a migrated thread and some comments may be shown as answers.
Entegral
Top achievements
Rank 1
Entegral asked on 02 Oct 2014, 11:12 AM
Hi

We would like to be able to set the expires header for files uploaded using the cloudupload, but I could not find any info on this in the Documentation. I am already implementing a Custom Amazon S3 Provider if this is of any use.

Thanks in advance.

4 Answers, 1 is accepted

Sort by
0
Hristo Valyavicharski
Telerik team
answered on 07 Oct 2014, 11:13 AM
Hi,

To set expire header of a file you will have to use Custom Amazon Provider and override the UploadFile() method:

public override void UploadFile(string keyName, System.Collections.Specialized.NameValueCollection metaData, System.IO.Stream fileStream)
{
     
    PutObjectRequest request = new PutObjectRequest() { Key = keyName, BucketName = BucketName, InputStream = fileStream };
 
    foreach (string key in metaData)
    {
        request.Metadata.Add(key, metaData[key]);
    }
 
    request.Headers.Expires = new DateTime(2014, 12, 1);
     
    try
    {
        //_s3Client.PutObject(request);
 
        AmazonS3Client.PutObject(request);
    }
    catch (AmazonS3Exception e)
    {
        var message = string.Format("Exception thrown for upload operation for file with keyName: {0}", keyName);
        throw new CloudUploadProviderException(message, e, keyName, BucketName);
    }
    
}

I hope this helps.

Regards,
Hristo Valyavicharski
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Entegral
Top achievements
Rank 1
answered on 24 Feb 2015, 01:31 PM
Hi Hristo.

Sorry for only coming back to you now. I have tried this method that you have supplied but there is not an expires header after upload.

Code:

Public Overrides Sub UploadFile(keyName As String, metaData As System.Collections.Specialized.NameValueCollection, fileStream As System.IO.Stream)
 
    Dim request As New PutObjectRequest() With { _
         .Key = keyName, _
         .BucketName = BucketName, _
         .InputStream = fileStream _
    }
 
    For Each key As String In metaData
        request.Metadata.Add(key, metaData(key))
    Next
 
    request.Headers.Expires = Date.Now.AddYears(30)
 
    Try
        '_s3Client.PutObject(request);
 
        AmazonS3Client.PutObject(request)
    Catch e As AmazonS3Exception
        Dim message = String.Format("Exception thrown for upload operation for file with keyName: {0}", keyName)
        Throw New CloudUploadProviderException(message, e, keyName, BucketName)
    End Try
 
End Sub

Regards
0
Hristo Valyavicharski
Telerik team
answered on 27 Feb 2015, 12:37 PM
Override the UploadOnSingleRequest method and set the CacheControl property:

public override void UploadFileOnSigleRequest(string keyName, System.Collections.Specialized.NameValueCollection metaData, System.IO.Stream fileStream)
 {
     _container = StorageContainer;
     var blob = _container.GetBlockBlobReference(keyName);
 
     //blob.Properties.ContentDisposition = "attachment; filename=\"test.pdf\"";
     //blob.Properties.ContentType = "text/plain";
 
     blob.Properties.CacheControl = "";
 
     try
     {
         blob.UploadFromStream(fileStream);
 
         for (int i = 0; i < metaData.AllKeys.Length; i++)
         {
             var key = metaData.GetKey(i);
             blob.Metadata.Add(key, metaData[key]);
         }
         blob.SetMetadata();
     }
     catch (Exception e)
     {
         var message = string.Format("Exception thrown for upload operation for file with keyName: {0}", keyName);
         throw new CloudUploadProviderException(message, e, keyName, BlobContainer);
     }
 }

The difference between cache control and expires header is explained here:
http://stackoverflow.com/questions/5799906/what-s-the-difference-between-expires-and-cache-control-headers

Regards,
Hristo Valyavicharski
Telerik
 

Check out the Telerik Platform - the only platform that combines a rich set of UI tools with powerful cloud services to develop web, hybrid and native mobile apps.

 
0
Sypher
Top achievements
Rank 1
answered on 02 Sep 2015, 07:49 PM

At least in v2015.2.826.45, the Telerik.Web.UI.AmazonS3Provider does not seem to include an UploadOnSingleRequest (or UploadOnSigleRequest) method. UploadFile and UploadChunk are available.

Tags
CloudUpload
Asked by
Entegral
Top achievements
Rank 1
Answers by
Hristo Valyavicharski
Telerik team
Entegral
Top achievements
Rank 1
Sypher
Top achievements
Rank 1
Share this question
or