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


76

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

问题是媒体文件的内容类型对于所有类型都设置为application / octet-stream。因此,媒体播放和进度栏中存在一些问题。

如何设置存储在Blob上的文件的适当Content-type(例如-video / mp4,video / ogg,video / webm)?

我不想通过blob界面手动为每个文件执行此操作。必须有其他我不知道的方法。可能是配置文件,设置文件等进行排序。或者,可能是一个代码块,用于为文件夹中存储的所有文件设置Content-type。

有什么建议?谢谢

Answers:


129

这应该工作:

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语句。


1
@smarx:是否也可以在CDN中做同样的事情,stackoverflow.com
Hitesh

1
没有可能直接在Azure门户上进行设置吗?没有编码
Emil

4
这是一篇博客文章,其中包含对mimetype映射的扩展的完整列表。ppolyzos.com/2015/10/27/...
dragon788

1
值得一提的是,如果您有权访问System.Web和.NET 4.5或更高版本,则可以调用MimeMapping.GetMimeMapping(“ somefile.mp4”)以获取内容类型。有关更多详细信息,请参见MSDN
大卫·耶茨

16

不幸的是,这里接受的答案目前不适用于最新的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();
blobHttpHeader.ContentType = "image/png";
 
var uploadedBlob = await blob.UploadAsync(YOURSTREAM, blobHttpHeader);

14

这是将视频以正确的Content-Type上传到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;
}

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 />";
}

1
到目前为止,这不适用于当前软件包。
凯里·理查森

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

随着Azure存储V10 SDK,斑点可以使用上传BlockBlobURL作为在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

By using our site, you acknowledge that you have read and understand our Cookie Policy and Privacy Policy.
Licensed under cc by-sa 3.0 with attribution required.