def add_auth_headers(self, headers, method, bucket, key, path_args): #如果headers头中不存在Date和Content-Type或其值为空时,则添加 if not headers.get("Date"): headers["Date"] = self.httpdate() if not headers.get("Content-Type"): headers["Content-Type"] = "" canonical_string = Utils.make_canonicalstring(method, bucket, key, path_args, headers, None) web.debug("canonical_string is %s" % canonical_string) encoded_canonical = Utils.encode(self.secret_access_key, canonical_string, False) auth = "AWS " + self.access_key_id + ":" + encoded_canonical #字符串连接 headers["Authorization"] = auth return headers
def generate_url(self, method, bucket, key, path_args, headers): expires = long(0) if self.expiresin: expires = long(round(time.time() * 1000)) + long(self.expiresin) #time.time()*1000获取当前时间的毫秒数 elif self.expires: expires = long(self.expires) else: raise Exception("Illegal expires state") #单位转换为秒 expires /= 1000 canonical_string = Utils.make_canonicalstring(method, bucket, key, path_args, headers, str(expires)) encoded_canonical = Utils.encode(self.secret_access_key, canonical_string, True) path_args = path_args if path_args else {} path_args["Signature"] = encoded_canonical path_args["Expires"] = str(expires) #将long型转换为string型 path_args["AWSAccessKeyId"] = self.access_key_id calling_format = Utils.get_callingformat_for_bucket(self.calling_format, bucket) if self.is_secure and not isinstance(calling_format, PathFormat) and bucket.find( "." ) != -1: raise Exception("You are making an SSL connection, however, the bucket contains periods and \ the wildcard certificate will not match by default. Please consider using HTTP.") #需要进行异常检查 try: return_string = calling_format.get_full_url(self.is_secure, self.server, self.port, bucket, key, path_args) except Exception as e: return_string = "Exception generating url " + e.strerror return return_string