def cdn_request(self, method, path=[], data='', hdrs=None): """ Given a method (i.e. GET, PUT, POST, etc), a path, data, header and metadata dicts, performs an http request against the CDN service. """ if not self.cdn_enabled: raise CDNNotEnabled() path = '/%s/%s' % \ (self.uri.rstrip('/'), '/'.join([unicode_quote(i) for i in path])) headers = { 'Content-Length': str(len(data)), 'User-Agent': self.user_agent, 'X-Auth-Token': self.token } if isinstance(hdrs, dict): headers.update(hdrs) def retry_request(): '''Re-connect and re-try a failed request once''' self.cdn_connect() self.cdn_connection.request(method, path, data, headers) return self.cdn_connection.getresponse() try: self.cdn_connection.request(method, path, data, headers) response = self.cdn_connection.getresponse() except (socket.error, IOError, HTTPException): response = retry_request() if response.status == 401: self._authenticate() headers['X-Auth-Token'] = self.token response = retry_request() return response
def make_public(self, ttl=consts.default_cdn_ttl): """ Either publishes the current container to the CDN or updates its CDN attributes. Requires CDN be enabled on the account. >>> container.make_public(ttl=604800) # expire in 1 week @param ttl: cache duration in seconds of the CDN server @type ttl: number """ if not self.conn.cdn_enabled: raise CDNNotEnabled() if self.cdn_uri: request_method = 'POST' else: request_method = 'PUT' hdrs = {'X-TTL': str(ttl), 'X-CDN-Enabled': 'True'} response = self.conn.cdn_request(request_method, [self.name], hdrs=hdrs) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason) self.cdn_ttl = ttl for hdr in response.getheaders(): if hdr[0].lower() == 'x-cdn-uri': self.cdn_uri = hdr[1]
def purge_from_cdn(self, email=None): """ Purge Edge cache for all object inside of this container. You will be notified by email if one is provided when the job completes. >>> container.purge_from_cdn("*****@*****.**") or >>> container.purge_from_cdn("[email protected],[email protected]") or >>> container.purge_from_cdn() @param email: A Valid email address @type email: str """ if not self.conn.cdn_enabled: raise CDNNotEnabled() if email: hdrs = {"X-Purge-Email": email} response = self.conn.cdn_request('DELETE', [self.name], hdrs=hdrs) else: response = self.conn.cdn_request('DELETE', [self.name]) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason)
def make_private(self): """ Disables CDN access to this container. It may continue to be available until its TTL expires. >>> container.make_private() """ if not self.conn.cdn_enabled: raise CDNNotEnabled() hdrs = {'X-CDN-Enabled': 'False'} self.cdn_uri = None response = self.conn.cdn_request('POST', [self.name], hdrs=hdrs) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason)
def is_public(self): """ Returns a boolean indicating whether or not this container is publically accessible via the CDN. >>> container.is_public() False >>> container.make_public() >>> container.is_public() True @rtype: bool @return: whether or not this container is published to the CDN """ if not self.conn.cdn_enabled: raise CDNNotEnabled() return self.cdn_uri is not None
def acl_referrer(self, cdn_acl_referrer=consts.cdn_acl_referrer): """ Enable ACL restriction by referrer for this container. >>> container.acl_referrer("http://www.example.com") @param cdn_acl_user_agent: Set the referrer ACL @type cdn_acl_user_agent: str """ if not self.conn.cdn_enabled: raise CDNNotEnabled() hdrs = {'X-Referrer-ACL': cdn_acl_referrer} response = self.conn.cdn_request('POST', [self.name], hdrs=hdrs) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason) self.cdn_acl_referrer = cdn_acl_referrer
def acl_user_agent(self, cdn_acl_user_agent=consts.cdn_acl_user_agent): """ Enable ACL restriction by User Agent for this container. >>> container.acl_user_agent("Mozilla") @param cdn_acl_user_agent: Set the user agent ACL @type cdn_acl_user_agent: str """ if not self.conn.cdn_enabled: raise CDNNotEnabled() hdrs = {'X-User-Agent-ACL': cdn_acl_user_agent} response = self.conn.cdn_request('POST', [self.name], hdrs=hdrs) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason) self.cdn_acl_user_agent = cdn_acl_user_agent
def log_retention(self, log_retention=consts.cdn_log_retention): """ Enable CDN log retention on the container. If enabled logs will be periodically (at unpredictable intervals) compressed and uploaded to a ".CDN_ACCESS_LOGS" container in the form of "container_name/YYYY/MM/DD/HH/XXXX.gz". Requires CDN be enabled on the account. >>> container.log_retention(True) @param log_retention: Enable or disable logs retention. @type log_retention: bool """ if not self.conn.cdn_enabled: raise CDNNotEnabled() hdrs = {'X-Log-Retention': log_retention} response = self.conn.cdn_request('POST', [self.name], hdrs=hdrs) if (response.status < 200) or (response.status >= 300): raise ResponseError(response.status, response.reason) self.cdn_log_retention = log_retention