How to stream the videos stored on google cloud storage bucket?

末鹿安然 提交于 2021-01-27 12:47:21

问题


I have stored videos on google cloud storage browser. I want to play these videos on my frontend. For that I need the video URL. But the problem is, whenever I navigate to that URL, the file gets downloaded.

What do I need to do to get the streaming video URL of my stored object?


回答1:


I accomplished this using video.js.

<html>
<head>
<link href="https://unpkg.com/video.js/dist/video-js.css" rel="stylesheet">
<style>
  .video-js {
    width: 600px;
    height: 600px;
  }
</style>
</head>
<body>
<video-js controls data-setup="{}">
  <source src="https://storage.cloud.google.com/bucketName/folderName/recordingName.flv" type="video/flv">
</video-js>

<script src="https://unpkg.com/video.js/dist/video.min.js"></script>
<script src="https://unpkg.com/videojs-flash/dist/videojs-flash.min.js"></script>
</body>
</html>



回答2:


Cloud Storage does support streaming transfers using gcloud. You can also do do streaming transfers on the backend of your application using the third party Boto python lib.

But since you want this solution to be implemented with a fronted I'm guessing you are actually looking for a media player.

So, you will need to:

1.- Generate a SignedUrl of the object (your video):

using gcloud command:

gsutil signurl -d 10m Desktop/private-key.json gs://example-bucket/cat.jpeg

using python lib:

from google.cloud import storage
import datetime

def generate_download_signed_url_v4(bucket_name, blob_name):
    """Generates a signed URL for downloading a blob.

    Note that this method requires a service account key file. You can not use
    this if you are using Application Default Credentials from Google Compute
    Engine or from the Google Cloud SDK.
    """
    storage_client = storage.Client.from_service_account_json('key.json')
    bucket = storage_client.get_bucket(bucket_name)
    blob = bucket.blob(blob_name)

    url = blob.generate_signed_url(
        # This URL is valid for 15 minutes
        expiration=datetime.timedelta(minutes=15),
        # Allow GET requests using this URL.
        method='GET'
        )

    print('Generated GET signed URL:')
    print(url)
    print('You can use this URL with any user agent, for example:')
    print('curl \'{}\''.format(url))
    return url

generate_download_signed_url_v4("example-id", "example.mp4")

2.- Initialize an HMTL5 media player with that url:

<!DOCTYPE html>
<html>
<body>
<video width="320" height="240" controls>
  <source src="video-signedurl.mp4" type="video/mp4">
Your browser does not support the video tag.
</video>
</body>



来源:https://stackoverflow.com/questions/57321691/how-to-stream-the-videos-stored-on-google-cloud-storage-bucket

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!