我们在Azure上托管了一个网站。它是基于媒体的,我们正在使用JWPlayer来播放带有HTTP伪流的媒体。媒体文件以3种格式存储在blob上 - mp4,ogg,webm。
问题是媒体文件的内容类型被设置为所有类型的application / octet-stream。因此,媒体播放和进度条中存在一些问题。
如何设置blob上存储的相应内容类型的文件(如 - video / mp4,video / ogg,video / webm)?
我不想通过进入blob界面手动为每个文件执行此操作。必须有其他方法来做这件事,我不知道。也许配置文件,设置文件等排序。或者也许是一个代码块,用于为存储在文件夹中的所有文件设置Content-type。
有什么建议吗? 感谢
答案 0 :(得分:108)
这应该有效:
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 :(得分:15)
不幸的是,这里接受的答案目前不适用于最新的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);
答案 2 :(得分:12)
这是使用正确的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;
}
答案 3 :(得分:1)
使用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 />";
}
答案 4 :(得分: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());
});
答案 5 :(得分:0)
使用Azure Storage v10 SDK,可以使用BlockBlobURL
作为instructed in the Node.js quickstart来上传Blob:
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
的{{1}}方法上传文件。