How to set Expire Http Header when uploading file

Oct 24, 2009 at 8:03 PM

I like to set one or more http headers when uploading a file (in addition to specify ACLs).  Is this possible?  I want to do this in one single round trip (i.e set the http headers upon upload). I can't seem to find the interface where I would pass a collection of http headers.

Thanks

Nov 2, 2009 at 4:16 AM

I've got this in my upload method...

const string defaultCacheControl = "max-age=864000"; // 10 days before the cache expires.

request.Headers.Add("Cache-Control", defaultCacheControl);
...not sure if it works (yet) but will tell you if it does.

Nov 3, 2009 at 11:15 PM

Here's my final working test method... You can't use the wrapper unless you modify the source of the wrapper yourself.

public static string UploadFile(string sourceFileName, string targetFileName)
{
    const string defaultAcl = "public-read-write";
    const string defaultCacheControl = "max-age=864000"; // 10 days before the cache expires.

    var config = new ThreeSharpConfig();
    config.AwsAccessKeyID = ConfigurationManager.AppSettings["AmazonS3AccessKeyId"];
    config.AwsSecretAccessKey = ConfigurationManager.AppSettings["AmazonS3SecretKeyId"];
    if (!string.IsNullOrEmpty(ConfigurationManager.AppSettings["WebProxy"]))
    {
        config.Proxy = new WebProxy(ConfigurationManager.AppSettings["WebProxy"]);
    }

    var service = new ThreeSharpQuery(config);

    try
    {
        using (var objectAddRequest = new ObjectAddRequest(ConfigurationManager.AppSettings["AmazonS3DefaultBucket"], targetFileName))
        {
            objectAddRequest.Headers.Add("x-amz-acl", defaultAcl);
            objectAddRequest.Headers.Add("Cache-Control", defaultCacheControl);
            objectAddRequest.LoadStreamWithFile(sourceFileName);
            using (var objectAddResponse = service.ObjectAdd(objectAddRequest))
            { return objectAddResponse.StatusCode.ToString(); }
        }
    }
    catch (Exception e)
    {
        Email.SendToApplicationErrorAccount(e);
        return e.Message;
    }
}

Feb 14, 2010 at 10:08 PM

Thanks for posting this - most helpful!

One question - why have you set ACL to public-read-write, rather than just public-read?