Пример #1
0
 def set_contents_from_filename(self, filename):
     if hasattr(self, 'content_type') and self.content_type is not None:
         mimetype = self.content_type
     else:
         mimetype, encoding = mimetypes.guess_type(filename)
     content_length = os.path.getsize(filename)
     if mimetype is not None:
         with open(filename, "rb") as data:
             self.blob_client.upload_blob(
                 data=data,
                 content_settings=ContentSettings(content_type=mimetype),
                 length=content_length,
                 overwrite=True)
     else:
         with open(filename, "rb") as data:
             self.blob_client.upload_blob(data=data,
                                          length=content_length,
                                          overwrite=True)
     self.get_properties()
     secs = (self.last_modified - epoch).total_seconds()
     os.utime(filename, (secs, secs))
Пример #2
0
def upload_entries(entries, blob_access):

    print(f'Container: {blob_access.container}')
    if(DRY_RUN):
        print("*** DRY RUN ***")
        for blob_entry in entries:
            print(f"""

    ---------------------------------------------------
    {blob_entry.path} {blob_entry.mime}
    """)
    else:
        blob_service_client, container_client = blob_clients(blob_access)
        for blob_entry in entries:
            print(f'upload {blob_entry.path}...')
            blob_client = blob_service_client.get_blob_client(container=blob_access.container, blob=blob_entry.path)
            blob_client.upload_blob(
                blob_entry.content,
                overwrite=True,
                content_settings=ContentSettings(content_type=blob_entry.mime))
            print('uploaded')
Пример #3
0
    def test_create_blob_from_stream_chunked_upload_with_count_and_properties(self):
        # Arrange
        blob_name = self._get_blob_reference()
        data = self.get_random_bytes(LARGE_BLOB_SIZE)
        with open(FILE_PATH, 'wb') as stream:
            stream.write(data)

        # Act
        content_settings=ContentSettings(
            content_type='image/png',
            content_language='spanish')
        blob_size = len(data) - 301
        with open(FILE_PATH, 'rb') as stream:
            self.bs.create_blob_from_stream(self.container_name, blob_name, stream, 
                                            blob_size, content_settings=content_settings)

        # Assert
        self.assertBlobEqual(self.container_name, blob_name, data[:blob_size])
        properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
        self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
        self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
Пример #4
0
    def _save(self, name, content):
        cleaned_name = clean_name(name)
        name = self._get_valid_path(name)
        guessed_type, content_encoding = mimetypes.guess_type(name)
        content_type = (_content_type(content) or guessed_type
                        or self.default_content_type)

        # Unwrap django file (wrapped by parent's save call)
        if isinstance(content, File):
            content = content.file

        content.seek(0)
        self.service.create_blob_from_stream(
            container_name=self.azure_container,
            blob_name=name,
            stream=content,
            content_settings=ContentSettings(
                content_type=content_type, content_encoding=content_encoding),
            max_connections=self.upload_max_conn,
            timeout=self.timeout)
        return cleaned_name
    def create_blob(self):
        container_name = self._create_container()

        # Basic
        # Create a blob with no data
        blob_name1 = self._get_blob_reference()
        self.service.create_blob(container_name, blob_name1)

        # Properties
        settings = ContentSettings(content_type='html', content_language='fr')
        blob_name2 = self._get_blob_reference()
        self.service.create_blob(container_name,
                                 blob_name2,
                                 content_settings=settings)

        # Metadata
        metadata = {'val1': 'foo', 'val2': 'blah'}
        blob_name2 = self._get_blob_reference()
        self.service.create_blob(container_name, blob_name2, metadata=metadata)

        self.service.delete_container(container_name)
Пример #6
0
    def test_create_blob_from_bytes_parallel_with_properties(self):
        # parallel tests introduce random order of requests, can only run live
        if TestMode.need_recording_file(self.test_mode):
            return        
        
        # Arrange
        blob_name = self._get_blob_reference()
        data = self.get_random_bytes(LARGE_BLOB_SIZE)

        # Act
        content_settings=ContentSettings(
                content_type='image/png',
                content_language='spanish')
        self.bs.create_blob_from_bytes(self.container_name, blob_name, data, 
                                       content_settings=content_settings, max_connections=5)

        # Assert
        self.assertBlobEqual(self.container_name, blob_name, data)
        properties = self.bs.get_blob_properties(self.container_name, blob_name).properties
        self.assertEqual(properties.content_settings.content_type, content_settings.content_type)
        self.assertEqual(properties.content_settings.content_language, content_settings.content_language)
Пример #7
0
    def fromtext(self, text, encoding='utf-8', mimetype='text/plain'):
        """ 
        set blob content from given text in StorageBlobModel instance. Parameters are:
        - text (required): path to a local file 
        - encoding (optional): text encoding (default is utf-8)
        - mimetype (optional): set a mimetype. azurestoragewrap will guess it if not given 
        """
        if isinstance(text, str):
            text = text.encode(encoding, 'ignore')

            # Load text into self.__content__
            self.content = bytes(text)

            self.properties.content_settings = ContentSettings(
                content_type=mimetype, content_encoding=encoding)

        else:
            raise AzureStorageWrapException(
                self,
                'Can not load blob content, because given text is not from type string'
            )
Пример #8
0
    def upload_to_cloud_storage(self,
                                temp_local_path: str,
                                blob_path: str,
                                content_type: str = None,
                                timeout=None):
        """
            Uploads the file in the given path to the Azure Blob storage service.
        :param temp_local_path:
        :param blob_path:
        :param content_type:
        :param timeout:
        :return:
        """
        blob_client = self.azure_service_client.get_blob_client(
            container=self.azure_container_name, blob=blob_path)
        my_content_settings = ContentSettings(content_type=content_type)

        with open(temp_local_path, "rb") as stream:
            blob_client.upload_blob(stream,
                                    overwrite=True,
                                    content_settings=my_content_settings)
Пример #9
0
 def store_file_from_memory(self,
                            key,
                            memstring,
                            metadata=None,
                            cache_control=None,
                            mimetype=None):
     if cache_control is not None:
         raise NotImplementedError(
             "AzureTransfer: cache_control support not implemented")
     key = self.format_key_for_backend(key, remove_slash_prefix=True)
     content_settings = None
     if mimetype:
         content_settings = ContentSettings(content_type=mimetype)
     self.conn.create_blob_from_bytes(
         self.container_name,
         key,
         bytes(
             memstring
         ),  # azure would work with memoryview, but validates it's bytes
         content_settings=content_settings,
         metadata=self.sanitize_metadata(metadata, replace_hyphen_with="_"))
Пример #10
0
def upload_icon(sas_url, file_path):
    # Break the SAS URL
    (scheme, netloc, path, params, query, fragment) = urlparse(sas_url)
    # Account is the first part of the netlocation upto the dot
    account_name = netloc[0:netloc.index('.')]

    # The assumption here is that the blob URL will be in the
    # form accountname.blob.core.windows.net or
    # accountname.blob.core.usgovcloudapi.net.
    # Chopping off accountname.blob. to obtain the
    # endpoint suffix.
    endpoint_suffix = netloc.replace(account_name + '.blob.', '')

    # Container name is the path
    container_name = path.strip('/')

    # Create a block blob service
    blockblob_service = BlockBlobService(account_name=account_name,
                                         sas_token=query,
                                         endpoint_suffix=endpoint_suffix)

    # Get the file name of the icon
    file_name = os.path.basename(file_path)
    # Determine the content type and encoding for the file
    (content_type, content_encoding) = mimetypes.guess_type(file_name)
    content_settings = ContentSettings(content_type=content_type,
                                       content_encoding=content_encoding)

    # Upload the icon
    blockblob_service.create_blob_from_path(container_name=container_name,
                                            blob_name=file_name,
                                            file_path=file_path,
                                            content_settings=content_settings)

    # Append the icon name to the path to generate the download link
    path = path + '/' + file_name
    urlparts = (scheme, netloc, path, params, query, fragment)
    sas_download_url = urlunparse(urlparts)

    return sas_download_url
Пример #11
0
def init_blob_for_streaming_upload(container: ContainerClient,
                                   blob_name: str,
                                   content_type: str,
                                   content_encoding: str,
                                   data: Any,
                                   return_sas_token: bool = True) -> str:
    """
    Uploads the given data to a blob record. If a blob with the given name already exist, it throws an error.

    Returns a uri with a SAS token to access the newly created blob.
    """
    create_container_using_client(container)
    logger.info(
        f"Streaming blob '{blob_name}' to container '{container.container_name}' on account: '{container.account_name}'"
    )

    content_settings = ContentSettings(content_type=content_type,
                                       content_encoding=content_encoding)
    blob = container.get_blob_client(blob_name)
    blob.stage_block()
    blob.commit_block_list()
    blob.upload_blob(data, content_settings=content_settings)
    logger.debug(f"  - blob '{blob_name}' uploaded. generating sas token.")

    if return_sas_token:
        sas_token = generate_blob_sas(blob.account_name,
                                      blob.container_name,
                                      blob.blob_name,
                                      account_key=blob.credential.account_key,
                                      permission=BlobSasPermissions(read=True),
                                      expiry=datetime.utcnow() +
                                      timedelta(days=14))

        uri = blob.url + "?" + sas_token
    else:
        uri = remove_sas_token(blob.url)

    logger.debug(f"  - blob access url: '{uri}'.")

    return uri
Пример #12
0
def save_file(file):
    print(file)

    # get image information
    filename = file.filename
    print(filename)
    file_name_split = os.path.splitext(filename)
    main_name = file_name_split[0]
    appliance_id = main_name.split("-")[0]
    usage = "livestream"
    # dt_modified = os.path.getmtime(file)
    # modified_date = datetime.datetime.fromtimestamp(
    #     dt_modified,
    #     pytz.timezone('US/Eastern')
    # ).isoformat()
    # modified_date_utc = datetime.datetime.utcfromtimestamp(
    #     dt_modified
    # ).isoformat()

    # dt_modified = str(datetime.datetime.now())
    dt_modified = ""
    modified_date = dt_modified
    modified_date_utc = dt_modified

    # save to blob
    blob_name = "live_stream_" + filename
    print(blob_name)

    block_blob_service.create_blob_from_stream(
        config.blob_container_image,
        blob_name,
        file,
        content_settings=ContentSettings(content_type='image/png'))

    blob_url = ("https://" + config.storage_account_name +
                ".blob.core.windows.net/" + config.blob_container_image + "/" +
                blob_name)
    print(blob_url)

    return appliance_id, blob_url, modified_date, modified_date_utc, usage
Пример #13
0
def upload_file_to_azure(file):

    try:

        dir_path = os.path.dirname(os.path.realpath(__file__))
        temp_dir = dir_path + '/' + file.filename
        file.save(dst=temp_dir)

        bbs.create_blob_from_path(
            container_name=app.config['AZURE_STORAGE_CONTAINER_NAME'],
            blob_name=file.filename,
            file_path=temp_dir,
            content_settings=ContentSettings(content_type=file.content_type))

        os.remove(temp_dir)

    except Exception as e:
        print("Something Happened: ", e)
        return e

    return "{}{}".format(app.config["AZURE_STORAGE_CONTAINER_LOCATION"],
                         file.filename)
Пример #14
0
def upload(local_file_name: str):
    """
    Upload to Azure using FTP
    https://github.com/Azure/azure-sdk-for-python/blob/master/sdk/storage/azure-storage-blob/azure/storage/blob/_blob_client.py#L375
    """
    az_key: str = os.getenv("AZ_STORAGE_KEY")
    az_string: str = os.getenv("AZ_STORAGE_CONNECTION_STRING")
    container_name: str = '$web'

    try:
        blob_service_client: BlobServiceClient = BlobServiceClient.from_connection_string(az_string)
        blob_client: BlobClient = blob_service_client.get_blob_client(container=container_name, blob=local_file_name)

        print("\nUploading to Azure Storage as blob:\n\t" + local_file_name)
        my_content_settings: ContentSettings = ContentSettings(content_type='text/html')

        with open('populated.html', "rb") as data:
            blob_client.upload_blob(data, overwrite=True, content_settings=my_content_settings)

    except Exception as ex:
        print('Exception:')
        print(ex)
Пример #15
0
def append_blob(container: ContainerClient,
                blob_name: str,
                content_type: str,
                content_encoding: str,
                data: Any,
                return_sas_token: bool = True,
                metadata: Dict[str, str] = None) -> str:
    """
    Uploads the given data to a blob record. If a blob with the given name already exist, it throws an error.

    Returns a uri with a SAS token to access the newly created blob.
    """
    create_container_using_client(container)
    logger.info(
        f"Appending data to blob '{blob_name}' in container '{container.container_name}' on account: '{container.account_name}'"
    )

    content_settings = ContentSettings(content_type=content_type,
                                       content_encoding=content_encoding)
    blob = container.get_blob_client(blob_name)
    try:
        props = blob.get_blob_properties()
        if props.blob_type != BlobType.AppendBlob:
            raise Exception('blob must be an append blob')
    except exceptions.ResourceNotFoundError:
        props = blob.create_append_blob(content_settings=content_settings,
                                        metadata=metadata)

    blob.append_block(data, len(data))
    logger.debug(f"  - blob '{blob_name}' appended. generating sas token.")

    if return_sas_token:
        uri = get_blob_uri_with_sas_token(blob)
    else:
        uri = remove_sas_token(blob.url)

    logger.debug(f"  - blob access url: '{uri}'.")

    return uri
Пример #16
0
    def test_get_set_blob_properties(self):
        # Arrange
        blob_name = self._get_blob_reference()
        self.bbs.create_blob_from_bytes(self.container_name,
                                        blob_name,
                                        b'AAABBBCCC',
                                        cpk=TEST_ENCRYPTION_KEY)

        # Act without the encryption key should fail
        with self.assertRaises(AzureHttpError):
            self.bbs.get_blob_properties(self.container_name, blob_name)

        # Act
        blob = self.bbs.get_blob_properties(self.container_name,
                                            blob_name,
                                            cpk=TEST_ENCRYPTION_KEY)

        # Assert
        self.assertTrue(blob.properties.server_encrypted)
        self.assertEqual(blob.properties.encryption_key_sha256,
                         TEST_ENCRYPTION_KEY.key_hash)

        # Act set blob properties
        self.bbs.set_blob_properties(
            self.container_name,
            blob_name,
            content_settings=ContentSettings(content_language='spanish',
                                             content_disposition='inline'),
            cpk=TEST_ENCRYPTION_KEY,
        )

        # Assert
        blob = self.bbs.get_blob_properties(self.container_name,
                                            blob_name,
                                            cpk=TEST_ENCRYPTION_KEY)
        self.assertEqual(blob.properties.content_settings.content_language,
                         'spanish')
        self.assertEqual(blob.properties.content_settings.content_disposition,
                         'inline')
Пример #17
0
def uploadtostorage():
        # Upload File to Blob
        uploaded_file = request.files['file1']
        block_blob_service.create_blob_from_path(
                'imagefiles',
                 uploaded_file.filename,
                'C:\\Users\\chvna\\Desktop\\Data\DP\\'+uploaded_file.filename,
                content_settings=ContentSettings(content_type='image/png')
        )

        imagename = uploaded_file.filename
        author = request.form['fileauthor1']
        title = request.form['filetitle1']
        rating = request.form['inputstar']
        today = datetime.datetime.now()
        conn = mysql.connect()
        cursor = conn.cursor()
        add_image = ("INSERT INTO image_blob(image_title,image_owner,image_name,image,created_date,rating) VALUES (%s, %s, %s, %s, %s, %s);")
        data_image = (title,author, imagename,storage_name+uploaded_file.filename,today,int(rating))
        cursor.execute(add_image, data_image)
        conn.commit()
        return render_template("upload.html",success1 = "File Uploaded SuccessFully")
Пример #18
0
 def store_file_from_disk(self,
                          key,
                          filepath,
                          metadata=None,
                          multipart=None,
                          cache_control=None,
                          mimetype=None):
     if cache_control is not None:
         raise NotImplementedError(
             "AzureTransfer: cache_control support not implemented")
     key = self.format_key_for_backend(key, remove_slash_prefix=True)
     content_settings = None
     if mimetype:
         content_settings = ContentSettings(content_type=mimetype)
     with open(filepath, "rb") as data:
         blob_client = self.conn.get_blob_client(self.container_name, key)
         blob_client.upload_blob(data,
                                 blob_type=BlobType.BlockBlob,
                                 content_settings=content_settings,
                                 metadata=self.sanitize_metadata(
                                     metadata, replace_hyphen_with="_"),
                                 overwrite=True)
Пример #19
0
def writeBlob(key):
    from azure.storage.blob import BlobServiceClient, ContentSettings

    service = BlobServiceClient(account_url="https://cptloadshed.blob.core.windows.net/", credential=key)

    blob = service.get_blob_client("stage", "current.json")

    data = {
        "Cape Town": city_sites.parseCpt(),
        "Johannesburg": city_sites.parseJhb(),
        "Durban": None,
        "Tshwane (Pretoria)": city_sites.parsePta()
    }

    with open("current.json", "w") as fout:
        print(json.dumps(data, indent=2), file = fout)

    with open("current.json", "rb") as fin:
        blob.upload_blob(fin, overwrite = True)
        blob.set_http_headers(ContentSettings(content_type = "application/json"))

    print("Successfully wrote blob")
    def test_create_blob_from_bytes_with_properties(self, resource_group,
                                                    location, storage_account,
                                                    storage_account_key):
        # parallel tests introduce random order of requests, can only run live

        self._setup(storage_account, storage_account_key)
        blob_name = self._get_blob_reference()
        blob = self.bsc.get_blob_client(self.container_name, blob_name)
        data = self.get_random_bytes(LARGE_BLOB_SIZE)

        # Act
        content_settings = ContentSettings(content_type='image/png',
                                           content_language='spanish')
        blob.upload_blob(data, content_settings=content_settings)

        # Assert
        self.assertBlobEqual(self.container_name, blob_name, data)
        properties = blob.get_blob_properties()
        self.assertEqual(properties.content_settings.content_type,
                         content_settings.content_type)
        self.assertEqual(properties.content_settings.content_language,
                         content_settings.content_language)
    def __init__(self, container: str, path: str = str(),
                 connection_string: str = STORAGE_CONNECTION_STRING,
                 content_type: Union[str, None] = DEFAULT_CONTENT_TYPE,
                 cache_control: str = DEFAULT_CACHE_CONTROL, compressed: bool = True,
                 content_disposition: Union[str, None] = None,
                 content_language: Union[str, None] = CONTENT_LANGUAGE,
                 tier: str = 'Hot', **kwargs):
        self.path = path
        self.compressed = compressed
        self._connection_string = connection_string
        self._container_name = container
        self._tier = getattr(StandardBlobTier, tier, None)
        self._lock = None

        if self._tier is None:
            raise ValueError(
                "Tier must be one of 'Hot', 'Cool' or 'Archive'. "
                "Got <%r> instead." % tier
            )

        self._content_settings: ContentSettings = ContentSettings(
            content_type=content_type,
            cache_control=cache_control,
            content_encoding="gzip" if self.compressed else None,
            content_language=content_language,
            content_disposition=content_disposition,
            **kwargs
        )

        self.client: AsyncBlobClient = AsyncBlobClient.from_connection_string(
            conn_str=connection_string,
            container_name=container,
            blob_name=path,
            # retry_to_secondary=True,
            connection_timeout=60,
            max_block_size=8 * 1024 * 1024,
            max_single_put_size=256 * 1024 * 1024,
            min_large_block_upload_threshold=8 * 1024 * 1024 + 1
        )
Пример #22
0
def publish(build, account, container, sas, **_) -> None:
    client = BlockBlobService(account_name=account, sas_token=sas)

    publishing_files = (
        p for p in glob.iglob(os.path.join(build, '**/*'), recursive=True))
    for source in publishing_files:
        if os.path.isdir(source):
            continue

        blob_path = os.path.join(os.environ['TRAVIS_REPO_SLUG'],
                                 os.environ['TRAVIS_BRANCH'],
                                 os.environ['TRAVIS_BUILD_NUMBER'],
                                 os.path.relpath(source, build))

        content_type, content_encoding = mimetypes.guess_type(
            os.path.basename(source))
        content_settings = ContentSettings(content_type, content_encoding)
        logger.info(f'Uploading {blob_path} ...')
        client.create_blob_from_path(container_name=container,
                                     blob_name=blob_path,
                                     file_path=source,
                                     content_settings=content_settings)
    def handle(self, *args, **options):
        content_settings = ContentSettings(content_type='application/json',
                                           content_encoding='gzip')

        block_blob_service = BlockBlobService(
            account_name=settings.AZURE_STORAGE_ACCOUNT_NAME,
            account_key=settings.AZURE_STORAGE_ACCOUNT_KEY)
        block_blob_service.create_container('heatmap',
                                            public_access=PublicAccess.Blob)
        block_blob_service.set_blob_service_properties(
            cors=[CorsRule(['*'], ['GET'])])

        block_blob_service.create_blob_from_path(
            'heatmap',
            'cluster.geojson',
            file_path=self.save_to_gzip_file(self.get_heatmap_cluster_data()),
            content_settings=content_settings)
        block_blob_service.create_blob_from_path(
            'heatmap',
            'community.geojson',
            file_path=self.save_to_gzip_file(self.get_community_data()),
            content_settings=content_settings)
Пример #24
0
def upload1():
    f = request.files['upload_files']
    file_name = f.filename
    comm = request.form['comments']
    print(file_name)
    newfile = "D:/Cloud_Assignments/Assignment10_Azure/" + file_name
    print(newfile)
    block_blob_service.create_blob_from_path(
        'saipriya',
        file_name,
        newfile,
        content_settings=ContentSettings(content_type='image/png'))
    imgUrl = 'https://mycloudassign.blob.core.windows.net/saipriya/' + file_name
    insertQuery = "insert into Photo (img) values ('%s')" % (imgUrl)
    print insertQuery
    cur = myConnection.cursor()
    cur.execute(insertQuery)
    query1 = 'select img from Photo'
    cur.execute(query1)
    res = cur.fetchall()
    myConnection.commit()
    return 'File uploaded successfully'
Пример #25
0
    def store_file_object(self,
                          key,
                          fd,
                          *,
                          cache_control=None,
                          metadata=None,
                          mimetype=None,
                          upload_progress_fn=None):
        if cache_control is not None:
            raise NotImplementedError(
                "AzureTransfer: cache_control support not implemented")
        key = self.format_key_for_backend(key, remove_slash_prefix=True)
        content_settings = None
        if mimetype:
            content_settings = ContentSettings(content_type=mimetype)

        def progress_callback(bytes_sent, _):
            if upload_progress_fn:
                upload_progress_fn(bytes_sent)

        # Azure _BlobChunkUploader calls `tell()` on the stream even though it doesn't use the result.
        # We expect the input stream not to support `tell()` so use dummy implementation for it
        original_tell = getattr(fd, "tell", None)
        fd.tell = lambda: None
        try:
            self.conn.create_blob_from_stream(
                self.container_name,
                key,
                fd,
                content_settings=content_settings,
                metadata=self.sanitize_metadata(metadata,
                                                replace_hyphen_with="_"),
                progress_callback=progress_callback)
        finally:
            if original_tell:
                fd.tell = original_tell
            else:
                delattr(fd, "tell")
Пример #26
0
def main():

    # Get credential
    parser = configparser.ConfigParser()
    parser.read('config.ini')
    STORAGE_ACCOUNT_NAME = parser.get('credential', 'STORAGE_ACCOUNT_NAME')
    STORAGE_ACCOUNT_KEY = parser.get('credential', 'STORAGE_ACCOUNT_KEY')
    CONTAINER_NAME_STRUCTUREDDATA = parser.get(
        'credential', 'CONTAINER_NAME_STRUCTUREDDATA')

    # access to blob storage
    block_blob_service = BlockBlobService(account_name=STORAGE_ACCOUNT_NAME,
                                          account_key=STORAGE_ACCOUNT_KEY)

    # load structured data from blob
    blob_text = block_blob_service.get_blob_to_text(
        CONTAINER_NAME_STRUCTUREDDATA, 'dataframe.tsv')
    #print(blob_text.content)
    # clean df
    df = pd.DataFrame.from_csv(StringIO(blob_text.content),
                               index_col=None,
                               sep='\t')
    print(df.shape)
    df_clean = df.dropna()
    print(df_clean.shape)

    # write cleaned dataframe to blob
    print("-----------write cleaned dataframe to blob------------")
    df_clean_str = df_clean.to_csv(sep='\t', index=False)

    dfblobname = 'dataframe_cleaned.tsv'
    settings = ContentSettings(content_type='text/tab-separated-values')
    block_blob_service.create_blob_from_text(CONTAINER_NAME_STRUCTUREDDATA,
                                             dfblobname,
                                             df_clean_str,
                                             content_settings=settings)

    return
Пример #27
0
    def write_to_azure(self):
        # check for internet
        # if not self.internet_on():
        #    return False

        # record file to cloud
        self.append_blob_service = AppendBlobService(
            account_name='navview',
            account_key=
            '+roYuNmQbtLvq2Tn227ELmb6s1hzavh0qVQwhLORkUpM0DN7gxFc4j+DF/rEla1EsTN2goHEA1J92moOM/lfxg==',
            protocol='http')
        self.append_blob_service.create_blob(
            container_name='data',
            blob_name=self.name,
            content_settings=ContentSettings(content_type='text/plain'))
        f = open("data/" + self.name, "r")
        self.append_blob_service.append_blob_from_text('data', self.name,
                                                       f.read())

        # TODO: check if success

        # record record to ansplatform
        self.record_to_ansplatform()
Пример #28
0
def upload_file_to_azure(base_64_image):
    """
    image upload to azure blob storage
    :param base_64_image:
    :return:
    """
    image_name = str(uuid.uuid4())

    base64_img_bytes = base_64_image.replace("data:image/png;base64,",
                                             "").encode('utf-8')
    blob_service_client = BlockBlobService(
        "travelsl",
        "xF0VCFJk6X3Nc+vUAz1sfraRzhj5gcg36BSurzHDOuu18YR/iEYnId/qxwyIpTksr0znuAV/F8Y4ExeGYBQtKw=="
    )
    blob_service_client.create_blob_from_text(
        "locationimages",
        image_name,
        base64.decodebytes(base64_img_bytes),
        encoding="base64",
        content_settings=ContentSettings(content_type="image/png",
                                         content_encoding="base64"))
    return "https://travelsl.blob.core.windows.net/locationimages/{}".format(
        image_name)
Пример #29
0
    async def _test_create_blob_from_bytes_with_properties(self):
        # parallel tests introduce random order of requests, can only run live
        await self._setup()
        if TestMode.need_recording_file(self.test_mode):
            return

        # Arrange
        blob_name = self._get_blob_reference()
        blob = self.bsc.get_blob_client(self.container_name, blob_name)
        data = self.get_random_bytes(LARGE_BLOB_SIZE)

        # Act
        content_settings = ContentSettings(content_type='image/png',
                                           content_language='spanish')
        await blob.upload_blob(data, content_settings=content_settings)

        # Assert
        await self.assertBlobEqual(self.container_name, blob_name, data)
        properties = await blob.get_blob_properties()
        self.assertEqual(properties.content_settings.content_type,
                         content_settings.content_type)
        self.assertEqual(properties.content_settings.content_language,
                         content_settings.content_language)
Пример #30
0
    async def test_creat_lrg_frm_stream_chnk_upload_w_cntnprops(
            self, resource_group, location, storage_account,
            storage_account_key):
        # parallel tests introduce random order of requests, can only run live
        if not self.is_live:
            pytest.skip("live only")

        # Arrange
        await self._setup(storage_account.name, storage_account_key)
        blob_name = self._get_blob_reference()
        blob = self.bsc.get_blob_client(self.container_name, blob_name)
        data = bytearray(urandom(LARGE_BLOB_SIZE))
        FILE_PATH = 'frm_stream_chnk_upload_w_cntnprops_async.temp.dat'
        with open(FILE_PATH, 'wb') as stream:
            stream.write(data)

        # Act
        try:
            content_settings = ContentSettings(content_type='image/png',
                                               content_language='spanish')
            blob_size = len(data) - 301
            with open(FILE_PATH, 'rb') as stream:
                await blob.upload_blob(stream,
                                       length=blob_size,
                                       content_settings=content_settings,
                                       max_concurrency=2)

            # Assert
            await self.assertBlobEqual(self.container_name, blob_name,
                                       data[:blob_size])
            properties = await blob.get_blob_properties()
            self.assertEqual(properties.content_settings.content_type,
                             content_settings.content_type)
            self.assertEqual(properties.content_settings.content_language,
                             content_settings.content_language)
        finally:
            self._teardown(FILE_PATH)