Example #1
0
        def run(self):
            while True:
                task = CloudApi.uploadQueue.get()
                if task:
                    self.url, self.callback = task
                    uploadParams = self.getUploadParams()
                    if uploadParams:
                        try:
                            form = MultiPartForm()
                            for name in uploadParams["params"]:
                                form.addField(name.encode('utf8'), uploadParams["params"][name].encode('utf8'))
                            
                            filePath = urlparse.urlparse(self.url).path
                            fp = open(filePath, 'rb')
                            form.addFile("file",os.path.basename(filePath) , fp)
                            body = str(form)

                            req = urllib2.Request(uploadParams["url"])
                            req.add_header('Content-type', form.getContentType())
                            req.add_header('Content-length', str(len(body)))
                            req.add_header('Accept', 'application/json')
                            req.add_data(body)
                            response = urllib2.urlopen(req)
                            if self.callback:
                                self.callback(json.load(response))
                        except urllib2.HTTPError, e:
                            print "HTTP Error Code:" + str(e.code)
                        except urllib2.URLError, e:
                            print e.reason
                        except IOError, e:
                            print "IOError when opening file to upload: "+filePath
Example #2
0
def upload(api_key, api_secret, project_id, filename, file):
    timestamp, dev_hash = _authenticate(api_secret)

    form = MultiPartForm()
    form.add_field('api_key', api_key)
    form.add_field('timestamp', timestamp)
    form.add_field('dev_hash', dev_hash)
    form.add_field('is_keeping_all_strings', 'true')
    form.add_field('file_format', 'IOS_STRINGS')

    form.add_file('file', filename, fileHandle=file)

    body = str(form)

    request = urllib2.Request(
        'https://platform.api.onesky.io/1/projects/%s/files' % (project_id))
    request.add_header('content-type', form.get_content_type())
    request.add_header('content-length', len(body))
    request.add_data(body)

    try:
        response = urllib2.urlopen(request)
        #print response.read()
    except urllib2.HTTPError as e:
        raise Exception(e.read())