设置存储在 Blob 上的媒体文件的内容类型

问题描述 投票:0回答:11

我们有一个托管在 Azure 上的网站。它是基于媒体的,我们使用 JWPlayer 通过 HTTP 伪流来播放媒体。媒体文件以 3 种格式存储在 blob 上 - mp4、ogg、webm。

问题是所有类型的媒体文件的内容类型都设置为应用程序/八位字节流。因此,媒体播放和进度条存在一些问题。

如何设置存储在 blob 上的文件的适当内容类型(例如 - video/mp4、video/ogg、video/webm)?

我不想通过进入 blob 界面手动为每个文件执行此操作。一定有其他我不知道的方法可以做到这一点。也许是配置文件、设置文件等。或者也许是一个代码块,用于为文件夹中存储的所有文件设置内容类型。

有什么建议吗? 谢谢

c# azure content-type jwplayer
11个回答
137
投票

这应该有效:

var storageAccount = CloudStorageAccount.Parse("YOURCONNECTIONSTRING");
var blobClient = storageAccount.CreateCloudBlobClient();

var blobs = blobClient
    .GetContainerReference("thecontainer")
    .ListBlobs(useFlatBlobListing: true)
    .OfType<CloudBlockBlob>();

foreach (var blob in blobs)
{
    if (Path.GetExtension(blob.Uri.AbsoluteUri) == ".mp4")
    {
        blob.Properties.ContentType = "video/mp4";
    }
    // repeat ad nauseam
    blob.SetProperties();
}

或者建立一个字典,这样你就不必编写一堆 if 语句。


78
投票

不幸的是,这里接受的答案目前不适用于最新的 SDK (12.x.+)

使用最新的 SDK,应通过 BlobHttpHeaders 设置内容类型。

var blobServiceClient = new BlobServiceClient("YOURCONNECTIONSTRING");
var containerClient = blobServiceClient.GetBlobContainerClient("YOURCONTAINERNAME");
var blob = containerClient.GetBlobClient("YOURFILE.jpg");

var blobHttpHeader = new BlobHttpHeaders { ContentType = "image/jpeg" };
 
var uploadedBlob = await blob.UploadAsync(YOURSTREAM, new BlobUploadOptions { HttpHeaders = blobHttpHeader });

YOURSTREAM 可能是

new BinaryData(byte[])


14
投票

这是使用正确的内容类型将视频上传到 Azure Blob 存储的工作示例:

public static String uploadFile(
     CloudBlobContainer container,String blobname, String fpath) {

    CloudBlockBlob blob;
    try {
        blob = container.getBlockBlobReference(blobname);
        File source = new File(fpath);

        if (blobname.endsWith(".mp4")) {
            System.out.println("Set content-type: video/mp4");
            blob.getProperties().setContentType("video/mp4");
        }

        blob.upload(new FileInputStream(source), source.length());

        return blob.getUri().toString();
    } catch (URISyntaxException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (StorageException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (FileNotFoundException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    } catch (IOException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

    return null;
}

7
投票

使用 Azure.Storage.Blogs (12.8.4), 我们可以如下设置文件的内容类型。 默认情况下,Azure 存储将文件存储在 application/octet-stream 中,如果是 *.svg 文件,则无法在 html 中正确呈现。 因此,在上传到 blob 时,我们必须将 *.svg 文件保存在 azure blob 存储中,内容类型为 image/svg+xml。

下面是我正在运行的代码示例。

  BlobServiceClient blobServiceClient = new BlobServiceClient("CONNECTIONSTRING");
  BlobContainerClient containerClient = blobServiceClient.GetBlobContainerClient("CONTAINERNAME");
  BlobClient blobClient = containerClient.GetBlobClient("BLOBNAME");
  try
  {

    Stream stream = file.OpenReadStream();
    await blobClient.UploadAsync(stream, true);
    blobClient.SetHttpHeaders(new BlobHttpHeaders() { ContentType = file.ContentType });
  }

标头上设置的 ContentType 应放置在 blobClient.UploadAsync() 的正下方。


2
投票

在Python中

azure_connection_str = libc.retrieve.get_any_secret('AZURE_STORAGE_CONNECTION')
blob_service_client = BlobServiceClient.from_connection_string(azure_connection_str)
blobs = blob_service_client.list_blobs()
my_content_settings = ContentSettings(content_type='video/mp4')

for blob in blobs:
    blob_client = blob_service_client.container_client.get_blob_client(blob)
    blob_client.set_http_headers(content_settings=my_content_settings)

1
投票

使用 Azure Storage v10 SDK,可以使用

BlockBlobURL
上传 blob,如 Node.js 快速入门中的指示:

const { Aborter, BlockBlobURL, ContainerURL, ServiceURL, SharedKeyCredential, StorageURL, uploadFileToBlockBlob } = require("@azure/storage-blob"); const containerName = "demo"; const blobName = "quickstart.txt"; const content = "hello!"; const credentials = new SharedKeyCredential( STORAGE_ACCOUNT_NAME, ACCOUNT_ACCESS_KEY ); const pipeline = StorageURL.newPipeline(credentials); const serviceURL = new ServiceURL( `https://${STORAGE_ACCOUNT_NAME}.blob.core.windows.net`, pipeline ); const containerURL = ContainerURL.fromServiceURL(serviceURL, containerName); const blockBlobURL = BlockBlobURL.fromContainerURL(containerURL, blobName); const aborter = Aborter.timeout(30 * ONE_MINUTE); await blockBlobURL.upload(aborter, content, content.length);

上传后可以使用

setHTTPHeaders

 方法设置内容类型:

// Set content type to text/plain await blockBlobURL.setHTTPHeaders(aborter, { blobContentType: "text/plain" });

可以使用

uploadFileToBlockBlob

中的
@azure/storage-blob
方法上传文件。


0
投票
使用php,可以通过如下设置内容类型来上传视频

$blobRestProxy = ServicesBuilder::getInstance()->createBlobService($connectionString); //upload $blob_name = "video.mp4"; $content = fopen("video.mp4", "r"); $options = new CreateBlobOptions(); $options->setBlobContentType("video/mp4"); try { //Upload blob $blobRestProxy->createBlockBlob("containername", $blob_name, $content, $options); echo "success"; } catch(ServiceException $e){ $code = $e->getCode(); $error_message = $e->getMessage(); echo $code.": ".$error_message."<br />"; }
    

0
投票
这就是我所做的

BlobHTTPHeaders h = new BlobHTTPHeaders(); String blobContentType = "image/jpeg"; h.withBlobContentType(blobContentType); blobURL.upload(Flowable.just(ByteBuffer.wrap(Files.readAllBytes(img.toPath()))), img.length(), h, null, null, null) .subscribe(resp-> { System.out.println("Completed upload request."); System.out.println(resp.statusCode()); });
    

0
投票
如果您中间有用于上传文件的API,您可以执行类似的操作。

使用

Azure.Storage.Files.Datalake v12.12.1

 和 Datalake storage Gen v2,您可以使用 
DataLakeFileUploadOptions
 指定内容类型。

using Azure.Storage.Files using Microsoft.AspNetCore.StaticFiles; (...) public async Task<IActionResult> UploadAsync(string container, string uploadDirectoryPath, IFormFile file) { var dataLakeServiceClient = new DataLakeServiceClient(connString); var dataLakeFileSystemClient = dataLakeServiceClient.GetFileSystemClient(container); var dataLakeFileClient = dataLakeFileSystemClient .GetFileClient(Path.Combine(uploadDirectoryPath, file.FileName)); var fileStream = file.OpenReadStream(); var mimeType = GetMimeType(file.FileName); var uploadOptions = new DataLakeFileUploadOptions() { HttpHeaders = new PathHttpHeaders() { ContentType = mimeType } }; await dataLakeFileClient.UploadAsync(fileStream, uploadOptions); return Ok(); } private string GetMimeType(string fileName) { var provider = new FileExtensionContentTypeProvider(); if (!provider.TryGetContentType(fileName, out var contentType)) { contentType = "application/octet-stream"; } return contentType; }
有关我正在使用的内容类型和 GetMimeType 方法的更多信息,请参见

此处



-1
投票

© www.soinside.com 2019 - 2024. All rights reserved.