S3 set expires header

5 posts, 0 answers
  1. Entegral
    Entegral avatar
    13 posts
    Member since:
    Nov 2006

    Posted 02 Oct 2014 Link to this post

    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.
  2. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 07 Oct 2014 Link to this post

    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.

     
  3. UI for ASP.NET Ajax is Ready for VS 2017
  4. Entegral
    Entegral avatar
    13 posts
    Member since:
    Nov 2006

    Posted 24 Feb 2015 in reply to Hristo Valyavicharski Link to this post

    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
  5. Hristo Valyavicharski
    Admin
    Hristo Valyavicharski avatar
    975 posts

    Posted 27 Feb 2015 Link to this post

    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.

     
  6. Sypher
    Sypher avatar
    79 posts
    Member since:
    Aug 2006

    Posted 02 Sep 2015 in reply to Hristo Valyavicharski Link to this post

    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.

Back to Top
UI for ASP.NET Ajax is Ready for VS 2017