public S3Request(S3Connection connection) { this.Connection = connection; this.AccessKey = connection.accessKey; this.SecretKey = connection.secretKey; this.Bucket = connection.bucket; this.IsUS = connection.IsUS; this.Host = connection.host; }
// This function throws an S3Exception in the event of a WebException status ProtocolError (HTTP error 404, 403, etc.). Other errors (timeouts, etc.) will be thrown as WebException. public S3Response Perform(Stream dataToSend) { Uri uri = MakeURI(); WebRequest request = WebRequest.Create(uri); #if DEBUG request.Timeout = 20000; // 20 seconds #endif request.Method = RequestMethodToString(Method); if (request.Headers["x-amz-date"] == null) { request.Headers.Add("x-amz-date", GetHttpDate()); } if (dataToSend != null) { request.ContentLength = dataToSend.Length; request.ContentType = "application/octet-stream"; // TODO: Be more sophisticated about setting this. if (EncryptionKey != null) { byte[] saltBytes = new byte[4]; RandomNumberGenerator.Create().GetBytes(saltBytes); uint salt = (uint)((saltBytes[3] << 24) | (saltBytes[2] << 16) | (saltBytes[1] << 8) | (saltBytes[0] << 0)); string keyHash = Crypto.CreateSaltedKeyHash(salt, EncryptionKey, false); KeySalt = Crypto.CreateSalt(16); request.Headers.Add("x-amz-meta-crypt", "aes_salted_" + keyHash); request.Headers.Add("x-amz-meta-crypt-salt", KeySalt); } } string canonicalString = MakeCanonicalString(Resource, request); string encodedCanonical = Encode(SecretKey, canonicalString, false); request.Headers.Add("Authorization", "AWS " + AccessKey + ":" + encodedCanonical); if (Method == RequestMethod.rmPut && dataToSend != null) { Stream dest = request.GetRequestStream(); if (EncryptionKey == null) { Spoonfeed(dataToSend, dest); } else { WriteEncryptedData(dataToSend, dest); } } try { return(new S3Response(request.GetResponse(), Connection)); } catch (WebException ex) { if (ex.Status == WebExceptionStatus.ProtocolError) { // Pull the string out here to make debugging easier because we won't be able to use the ResponseStream again. string responseString = S3Connection.SlurpInputStream(ex.Response.GetResponseStream()); // We do it this way so we can inspect the response. throw new S3Exception(responseString); } else { throw ex; } } }
internal S3Response(WebResponse response, S3Connection connection) { this.webResponse = response; this.connection = connection; }
public JungleDiskConnection(string accessKey, string secretKey) { s3 = new S3Connection(accessKey, secretKey); s3.passwordRequiredDelegate = PasswordRequiredFromS3; }